文件读写1

    xiaoxiao2021-03-25  169

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class Filedemo { public static void main(String[] args) { File file = new File("test.txt"); //文件存在 if (file.exists()) {

    try {

    //读取文件数据到缓冲区

    FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr);

         //写入新文件

    File newfile = new File("newtest.txt"); FileOutputStream fos = new FileOutputStream(newfile); OutputStreamWriter osw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(osw); String line; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine();//分行 } bw.close(); osw.close(); fos.close(); br.close(); isr.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } System.err.println("exists"); } }
    转载请注明原文地址: https://ju.6miu.com/read-1005.html

    最新回复(0)