java读入输出文件字符串

来源:互联网 发布:网络虚拟财产保护2017 编辑:程序博客网 时间:2024/06/06 00:49
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class file {

        /**
        * @param args
        * @throws IOException
        */
       
        public static void  read() throws IOException{
              String fileName = "Compound.txt";
              File file= new File(fileName);
               //打开文件和关闭文件一次
              BufferedReader reader = new BufferedReader(new FileReader(file));
              BufferedWriter writer = new BufferedWriter(new FileWriter(new File("rs.txt")));
              String tempString = null;
               try {
                     tempString = reader.readLine();
              } catch (IOException e1) {
                      // TODO Auto-generated catch block
                     e1.printStackTrace();
              }
               while(tempString!= null&&tempString.length()>0){       
               //分割字符串
           String str1= null;
           String str2= null;
           String[] subs = tempString.split( "\t");
           str1=subs[0];
           str2=subs[1];
           String str3=str2.substring(59);  
           System. out.println(str1+"\t" +str3);
           //write
           try{
                 writer.write(str1+ "\t"+str3);    
                 writer.write( "\n");
              } catch(Exception e){
            }
           tempString = reader.readLine();
              }
              writer.close();
              reader.close();

       }

原创粉丝点击