io读取操作

    public void writeFile(String filePathAndName) {
        try {
            FileInputStream fis = new FileInputStream(new File(filePathAndName));
            StringBuffer content = new StringBuffer();
            DataInputStream in = new DataInputStream(fis);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

            // BufferedReader reader = new BufferedReader(new
            // InputStreamReader(new FileInputStream(new File(file)), "UTF-8"));
            String str = null;
            while ((str = reader.readLine()) != null) {
                content.append(str);
            }

            File f = new File(filePathAndName+".c");
            if (!f.exists()) {
                f.createNewFile();
            }
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(content.toString());
            writer.close();
            
        } catch (Exception e) {
            System.out.println("写文件内容操作出错");
            e.printStackTrace();
        }
    }
 

你可能感兴趣的:(IO)