常见IO操作
2020-11-22 23:26 Spiderman25 阅读(100) 评论(0) 收藏 举报1 public class MyTest { 2 3 public static void main(String[] args) throws IOException { 4 String readPath="/readPath/x.txt"; 5 String writePath="/writePath/t.txt"; 6 File file=new File(readPath); 7 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "UTF-8"); 8 BufferedReader br = new BufferedReader(inputStreamReader); 9 //BufferedReader br = new BufferedReader(new FileReader(file)); 10 StringBuilder result=new StringBuilder(); 11 result.append("标题"); 12 result.append(System.lineSeparator()); 13 String s; 14 while ((s=br.readLine())!=null){ 15 result.append(s); 16 result.append(System.lineSeparator()); 17 //result.append("\r\n");//windows 18 //result.append("\n");//linux 19 //result.append("\r");//mac 20 } 21 br.close(); 22 write(writePath,result.toString()); 23 } 24 public static void write(String path,String content) throws IOException { 25 BufferedWriter bw=new BufferedWriter(new FileWriter(path)); 26 bw.write(content); 27 bw.newLine();//这也可以换行 28 bw.close(); 29 } 30 }
Scanner:https://blog.csdn.net/qq_40164190/article/details/81917208
1 public Object deepClone() throws IOException, ClassNotFoundException { 2 3 /* 写入当前对象的二进制流 */ 4 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 5 ObjectOutputStream oos = new ObjectOutputStream(bos); 6 oos.writeObject(this); 7 8 /* 读出二进制流产生的新对象 */ 9 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 10 ObjectInputStream ois = new ObjectInputStream(bis); 11 return ois.readObject(); 12 }
浙公网安备 33010602011771号