读取 写入 文件import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class text6 {public static void main(String[] args) {System.out.println(fiel_read("file\\a.txt"));String str="abcdefg123456";file_write("file2","/a.txt",str);}public static String fiel_read(String dirPath) { //读取文件StringBuffer retstr = new StringBuffer();try (FileInputStream fis = new FileInputStream(dirPath);) {while (true) {//利用read(byte[])+循环读取文件byte[] ss = new byte[1024];int n = fis.read(ss);if (n == -1) {break;}for (int i = 0; i < n; i++) {//System.out.println((char)ss[i]);retstr.append((char) ss[i]);}}//fis.close();} catch (Exception e) {System.out.println(e.toString());}return retstr.toString();}public static void file_write(String dirPath,String name,String data) { //写入文件File file = new File(dirPath);if (!file.exists() && !file.isDirectory()) {file.mkdirs();}try (FileOutputStream fos=new FileOutputStream(dirPath+name,true);){fos.write(data.getBytes());//fos.close();}catch (Exception e){System.out.println(e.toString());}}}