Java文件读写

java文件操作

public class Something {
	public static void main(String[] args) throws Exception {
		String filePathIn = "C:\\Users\\liuyalong\\Desktop\\StudyJava\\other\\src\\main\\java\\a.txt";
		String filePathOut = "C:\\Users\\liuyalong\\Desktop\\StudyJava\\other\\src\\main\\java\\b.txt";
		FileInputStream fileInputStream = new FileInputStream(filePathIn);
		FileOutputStream fileOutputStream = new FileOutputStream(filePathOut);

		BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream, 1024);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 1024);
                    // 临时数组,用来存即将操作的数据
		byte[] bytes = new byte[1024];
		int length;
		while ((length = bufferedInputStream.read(bytes)) != -1) {
			bufferedOutputStream.write(bytes, 0, length);
			// 这里可以立即刷新写入磁盘,否则会丢失数据.因为只有缓冲区满了,才会写入磁盘.
                            // 但是如果调用了BufferedOutputStream 的close()方法,会自动flush,也就不用手动flush了
			bufferedOutputStream.flush();
			System.out.print(new String(bytes, 0, length));
		}
                    // close可以用 try-with-resources语法自动关闭
                    //  try ( FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
                    //         BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream, 1024))
                    bufferedInputStream.close();
                    bufferedOutputStream.close();
		fileInputStream.close();
		fileOutputStream.close();


	}

}

修改文件/读取部分文件RandomAccessFile

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

class DemoApplicationTests {

    private static final String charSet = "GBK";
    private static final String fileName = "a.txt";

    public static void main(String[] args) {

        readFile();
    }

    public static void readFile() {
        try (RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "r");) {
            while (true) {
                //读完一行后会自动seek到下一行开始位置
                String buffer = randomAccessFile.readLine();
                if (buffer == null) return;
                //需要转换编码输出,readLine是用`ISO_8859_1`编码的
                byte[] originalBytes = buffer.getBytes(StandardCharsets.ISO_8859_1);
                String utf8 = new String(originalBytes, charSet);
                System.out.println(utf8);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeFile() {

        // 写模式下,如果文件不存在,会自动创建文件
        try (RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rwd")) {
            // 如果追加写入,要先移动到文件末尾,如果seek到文件中间再写入,会覆盖seek位置后面相同大小的内容
            randomAccessFile.seek(randomAccessFile.length());
            for (int i = 0; i < 109; i++) {
                //write后会自动seek到结尾位置,下次write不需要seek
                randomAccessFile.write((i + "\t乌漆麻黑qwrewtuhcgdjbnkdxv\n").getBytes(charSet));
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
posted @ 2020-12-09 15:02  rm-rf*  阅读(94)  评论(0编辑  收藏  举报