public static void TestBufferedWriter(){ try{ String filePath = "E:\\upload\\glentry\\test.txt"; File myFile = new File(filePath); if( !myFile.exists() ){ myFile.createNewFile(); } FileWriter resultFile = new FileWriter(myFile); BufferedWriter buffer = new BufferedWriter(resultFile); String fileContent = "This is my name.\n we are isoftsotne.com \n chinesebillboy at here。\n"; String contentString = ""; int flag = 0; boolean isGO = true; while((flag = fileContent.indexOf("\n")) != -1 && isGO ){ contentString = fileContent.substring(0,flag); buffer.write(contentString); buffer.newLine(); buffer.flush(); if( flag+2 >= fileContent.length()){ isGO = false; }else{ fileContent = fileContent.substring(flag+2); } } buffer.flush(); resultFile.flush(); buffer.close(); resultFile.close(); }catch(IOException ioe){ ioe.printStackTrace(); }finally{ } }
FileWriter(String) 写文件换行的方法。
最新推荐文章于 2023-01-13 13:39:12 发布