java读取文件,写入换行

FileReader fr = null;
		FileWriter fw=null;
		try {
			fr = new FileReader("english.txt");
			fw= new FileWriter("english2.txt");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		BufferedReader bf=new BufferedReader(fr);		
		BufferedWriter bw=new BufferedWriter(fw);		
		String str=null;//取一行
		StringBuffer strb = null;//对string的插入操作需要用StringBuffer类型		
		try {
			while((str=bf.readLine())!=null){  //一行一行的读取全部内容
				strb=new StringBuffer(str);
				if(str.lastIndexOf(" ")!=-1) { //判断是否存在 空格符
					strb.insert(strb.indexOf(" "), "|");
				}
				else{
					for(int i=0;i<strb.length();i++){
						if(strb.charAt(i)>122)
							strb.insert(i,"|");
					}
				}
				fw.write(strb.toString());
				fw.write("\r\n");//写入换行
			}		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}

你可能感兴趣的:(java)