publicclassRandomFile{ publicstaticvoidmain(String[] args)throws Exception{ //创建File对象demo File demo = new File("src/flag.txt"); //创建RandomAccessFile对象 RandomAccessFile raf = new RandomAccessFile(demo,"rw"); //定义每次读取的字节数 byte[] b = newbyte[1024]; //用于判断是否读写成功 int a = -1; //创建二进制输出流对象 ByteArrayOutputStream out = new ByteArrayOutputStream(); //循环读取 while((a=raf.read(b))!=-1){ out.write(b,0,a); } //输出 System.out.println(out.toString()); //关闭文件 raf.close(); } }
RandomAccessFile写文件
1 2 3 4
RandomAccessFile demo1 = new RandomAccessFile("src/flag.txt","rw"); String content = "flag is stolen by me"; demo1.write(content.getBytes(StandardCharsets.UTF_8)); demo1.close();
FileInputStream&&FileOutputStream
FileInputStream和FileOutputStream都是采用字节流操作文件文件
FileInputStream读取文件
跟RandomAccessFile读取文件大同小异
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassInputStreamTest{ publicstaticvoidmain(String[] args)throws IOException { //创建FileInputStream对象 FileInputStream fileInputStream = new FileInputStream("src/flag.txt"); byte[] bytes = newbyte[1024]; int a = -1; ByteArrayOutputStream out = new ByteArrayOutputStream(); //循环读出文件内容 while((a=fileInputStream.read(bytes))!=-1){ out.write(bytes,0,a); } System.out.println(out); } }
FileOutputStream写入文件
1 2 3 4 5 6 7 8 9 10 11 12
publicclassOutputStreamTest{ publicstaticvoidmain(String[] args)throws IOException { //创建FileOutStream对象 FileOutputStream fileOutputStream = new FileOutputStream(new File("src/flag.txt")); String content = "file was modified by me--Hack"; //以字节形式输入字节流 fileOutputStream.write(content.getBytes(StandardCharsets.UTF_8)); //将缓存的数据发出去 fileOutputStream.flush(); fileOutputStream.close(); } }
public class BufferStream { public static void main(String[] args) throws Exception{ //创建BufferedInputStream对象,以FileInputStream对线为参数 BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("src/flag.txt")); //输出文件内容 int a = -1; byte[] b = new byte[1024]; ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((a=bufferedInputStream.read(b))!=-1){ out.write(b,0,a); } System.out.println(out.toString()); } }
BufferedOutputStream写入文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassBufferStream{ publicstaticvoidmain(String[] args)throws Exception{ BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("src/flag.txt")); //要写入的字符串 String content = "Modified by me again:)"; //写入字符串的字节流 bufferedOutputStream.write(content.getBytes(StandardCharsets.UTF_8)); //将缓冲区的内容强制发送到文件 bufferedOutputStream.flush(); //关闭文件 bufferedOutputStream.close();
} }
InputStreamReader&&OutputStreamWriter
InputStreamReader完成byte流解析为char流,按照编码解析
OutputStreamWriter提供char流到byte流,按照编码处理
InputStreamReader读取文件
1 2 3 4 5 6 7 8 9 10 11 12 13
publicclassInputReaderTest{ publicstaticvoidmain(String[] args)throws Exception{ //创建InputStreamReader实现从byte流到char流的编码解析,此处指定编码为UTF-8 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("src/flag.txt"), StandardCharsets.UTF_8); char[] strings = newchar[1024]; int a = -1; StringBuilder stringBuilder = new StringBuilder(); while((a=inputStreamReader.read(strings))!=-1){ stringBuilder.append(strings,0,a); } System.out.println(stringBuilder); } }
OutputStreamWriter写入文件
1 2 3 4 5 6 7 8 9
publicclassOutputWriter{ publicstaticvoidmain(String[] args)throws Exception{ OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("src/flag.txt")); String content = "this is writen by OutputStreamWriter!"; outputStreamWriter.write(content); outputStreamWriter.flush(); outputStreamWriter.close(); } }
同样flush()用于强制写入文件
BufferedReader&BufferedWriter&PrintWriter
BufferedReader&BufferedWriter特点是
读取字符流
支持读取/写入一行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassBuffTest{ publicstaticvoidmain(String[] args)throws Exception { //创建BufferReader对象,由FileInputStream=>InputStreamReader=>BufferedReader BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("src/flag.txt"))); //创建BufferedWriter对象,由FileOutputStream=>OutputStreamReader=>BufferedReader BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/test.txt"))); String line = ""; //逐行读取flag.txt的内容再逐行写入test.txt while ((line=bufferedReader.readLine())!=null){ bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.flush(); } //关闭Reader和Writer bufferedReader.close(); bufferedWriter.close();
} }
注意:通过BufferedWriter写入的时候不会自动换行,需要使用newLine()函数换行
通过PrintWriter写入文件如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassBuffTest{ publicstaticvoidmain(String[] args)throws Exception { //创建BufferReader对象,由FileInputStream=>InputStreamReader=>BufferedReader BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("src/flag.txt"))); PrintWriter printWriter = new PrintWriter("src/test.txt"); String line = ""; //逐行读取flag.txt的内容再逐行写入test.txt while ((line=bufferedReader.readLine())!=null){ printWriter.println(line); printWriter.flush(); } //关闭Reader和PrintWriter bufferedReader.close(); printWriter.close();