Java学习-19天

Java学习-19天,第1张

Java学习-19天

package IO;

import java.io.FileInputStream;
import java.io.IOException;


public class _01_FileInputStream_01 {
    public static void main(String[] args) {
        // 创建字节输入流
        // 传入文件路径,用于打开该文件获取数据
        // 绝对
        // FileInputStream fis = new FileInputStream("D:\a.txt");
        FileInputStream fis1=null;
        try {
            // 相对
            // 在eclipse中,定位不是当前文件,而是当前项目
            fis1 = new FileInputStream("./src/b.txt");
            int data = 0;
            // read :读取一个字节,并返回对应的ASCII值, 返回为int类型,如果到达文件末尾(读完了)返回-1
            while ((data = fis1.read()) != -1) {
                System.out.print(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            // 需要关闭
            try {
            if (fis1 != null) {
                fis1.close();
            }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }
}

package IO;

import java.io.FileInputStream;
import java.io.IOException;

public class _01_FileInputStream_02 {
public static void main(String[] args) {
    //自动关闭资源
    try(FileInputStream    fis1 = new FileInputStream("./src/b.txt");) {
        int data = 0;
        while ((data = fis1.read()) != -1) {
            System.out.print(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

package IO;

import java.io.FileInputStream;


public class _01_FileInputStream_03 {
    public static void main(String[] args) {
        try(FileInputStream fis = new FileInputStream("./src/b.txt")) {
            //可获取的字节数
            System.out.println(fis.available());
            //数组容量并不是越大效率越高,容量和数据大小刚好一致的时候,效率最高
            byte[] bytes = new byte[fis.available()];
            int temp = 0;
            while ((temp = fis.read(bytes)) != -1) {
                //把字节数组,可能会出现数据重复问题
                //System.out.println(new String(bytes));
                //因为read返回的是读取元素的个数,所以我们可以指定读多少转换多少
                //数组,起始位置(包含),个数
                System.out.println(new String(bytes,0,temp));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

package IO;

import java.io.FileReader;
import java.io.IOException;


public class _02_FileReader_01 {
    public static void main(String[] args) {
        try (FileReader fr = new FileReader("./src/b.txt");){
            int temp = 0;
            char[] chars = new char[10];
            while ((temp = fr.read(chars)) != -1) {
                System.out.print(new String(chars,0,temp));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

package IO;

import java.io.FileOutputStream;
import java.io.IOException;


public class _03_FileOutStream_01 {
    public static void main(String[] args) {
        try(FileOutputStream fos= new FileOutputStream("D:/com/a.txt")) {
            //写出对应的ASCII码
            fos.write(97);
            //写出数组
            byte[] bytes = {97,98,99};
            fos.write(bytes);
            //不能直接写出字符串,可以通过转byte数组写出
            String str = "你好吗";
            fos.write(str.getBytes());
            //刷新缓存,强制持久化
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

package IO;

import java.io.FileOutputStream;
import java.io.IOException;

public class _03_FileOutStream_02 {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {

        //覆盖写入,当穿件该文件的输出流对象的时候,就会把该文件中内容全删除
        //FileOutputStream fos= new FileOutputStream("D:/com/a.txt");
        //创建对象时,传入第二个参数true,说明是追加写入,则不会清空字符串
        FileOutputStream fos= new FileOutputStream("D:/com/a.txt",true);
        fos.write(97);

}
}

package IO;

import java.io.FileWriter;
import java.io.IOException;

public class _04_FileWriter_01 {
public static void main(String[] args) {
    //用法和字节输出一致,只不过新增了可以写出字符串的方法重载
    try (FileWriter fw= new FileWriter("D:/com/b.txt")) {
        //写出字符数组
        char[] chars= {'a','v'};
        fw.write(chars);
        //可以直接写出字符串
        fw.write("你好吗");
        fw.flush();
    } catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}
}

package IO;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class _05_BufferedReader_01 {
    public static void main(String[] args) {
        try (
            //创建字符输出流
            FileReader fr = new FileReader("D:/com/c.txt");
            //创建字符输入缓冲流
            BufferedReader br = new BufferedReader(fr);){
            //br.readLine(): 读取一行数据,返回读取到的这一行数据的内容,为String,到达文件末尾返回null
            String temp= null;
            while  ((temp = br.readLine()) != null){
                System.out.println(temp);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }
}

package IO;

import java.io.BufferedWriter;
import java.io.FileWriter;

public class _06_BufferedWrite {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        try {
            //创建字符输出流
            FileWriter fw = new FileWriter("D:/a.txt");
            //创建字符输出缓冲流
            BufferedWriter bw = new BufferedWriter(fw);
            //写出
            bw.write("你好吗");
            //换行
            bw.newline();
            bw.write("???");
            //刷缓存
            bw.flush();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

package IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

public class _07_InputStreamReader {
    public static void main(String[] args) {
    try (FileInputStream fis = getInputStream("D:/a.txt");
    // 使用转换流把字节输入流转换为字符输入流
            InputStreamReader isr = new InputStreamReader(fis);) {
        int temp = 0;
        while ((temp = isr.read()) != -1) {
            System.out.print((char) temp);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// 需求 : 根据文件路径,获取文件的输入流对象
public static FileInputStream getInputStream(String filePath)
        throws FileNotFoundException {
    return new FileInputStream(filePath);
}}
 

package IO;

import java.io.FileOutputStream;
import java.io.PrintStream;


public class _08_PrintStream {
    public static void main(String[] args) throws Exception {
        //创建输出流
        FileOutputStream fos = new FileOutputStream("D:/a.txt");
        //封装为打印流
        PrintStream ps =new PrintStream(fos);
        ps.println(123);
        ps.println("aofnbs2e");
        
        //System 中的打印流,默认打印到控制台
        System.out.println("xxx");
        //更改System中的打印流
        System.setOut(ps);
        
        //一下所有打印 *** 作,不显示在控制台中,而是打印到指定文件中
        System.out.println("222222");
        System.out.println("你好");
    }
}

package IO;

import java.io.DataOutputStream;
import java.io.FileOutputStream;


public class _09_DataOutputStream {
    public static void main(String[] args) throws Exception{
        //创建对象
        DataOutputStream  dos = new DataOutputStream(new FileOutputStream("D;/a.txt"));
        //写出数据
        dos.writeByte(1);
        dos.writeShort(12);
        dos.writeInt(51);
        dos.writeUTF("sbdaiusduh");
        
        dos.flush();
        dos.close();
    }
}
        

package IO;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class _10_DataInputStream {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream("D;/a.txt"));
    
    // 必须保证 顺序和类型一致
    System.out.println(dis.readByte());
    System.out.println(dis.readShort());
    System.out.println(dis.readInt());
    System.out.println(dis.readUTF());
}
}
 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5710208.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存