Java 文件读写操作示例:将字符串写入文件并读取
public static void fileout() throws FileNotFoundException {\nFile file=new File("D:/file2/result2.txt");\nFileOutputStream out=new FileOutputStream(file);\nString str="小明-18-90;小红-17-95;老刚-29-60";\n\ttry {\n\t out.write(str.getBytes());\n\t} catch (IOException e) {\n\t e.printStackTrace();\n\t}\n\ttry {\n\t out.close();\n\t} catch (IOException e) {\n\t e.printStackTrace();\n\t}\n}\npublic static void filein() throws FileNotFoundException {\n\tFile file = new File("D:/file2/result2.txt");\n\tFileInputStream in = new FileInputStream(file);\n\ttry {\n\t int content;\n\t while ((content = in.read()) != -1) {\n\t System.out.print((char) content);\n\t }\n\t} catch (IOException e) {\n\t e.printStackTrace();\n\t}\n\ttry {\n\t in.close();\n\t} catch (IOException e) {\n\t e.printStackTrace();\n\t}\n}
原文地址: https://www.cveoy.top/t/topic/p7rm 著作权归作者所有。请勿转载和采集!