【问题标题】:Java - File manipulationJava - 文件操作
【发布时间】:2012-12-17 17:23:37
【问题描述】:

我在输入文件 a.txt 中有一些内容为

Line 1 : "abcdefghijk001mnopqr hellohello"
Line 2 : "qwertyuiop002asdfgh welcometologic"
Line 3 : "iamworkingherefromnowhere002yes somethingsomething"
Line 4 : "thiswillbesolved001here ithink"

我必须读取 a.txt 文件并将其写入两个单独的文件。即,具有 001 的行应写入 output1.txt,具有 002 的行应写入 output2.txt

谁能帮我解释一下 Java 编程中的逻辑。

谢谢, 娜仁

【问题讨论】:

  • 欢迎来到 Stackoverflow。你的问题很好。如果有任何错误或逻辑错误等,请谷歌尝试并发布您的代码以在此处解决。

标签: java logic


【解决方案1】:
BufferedReader br = new BufferedReader( new FileReader( "a.txt" ));
String line;
while(( line = br.readLine()) != null ) {
    if( line.contains( "001" )) sendToFile001( line );
    if( line.contains( "002" )) sendToFile002( line );
}
br.close();

方法sendToFile001()和sendToFile002()的参数行写法如下:

ps001.println( line );

使用 PrintStream 类型的 ps001 和 ps002,之前打开(在构造函数中?)

【讨论】:

  • 如果我这样做,我不会写所有行,只写最后一个迭代行
【解决方案2】:

这是Reading and writing text files using Java 的一个很好的例子,检查条件如下

while ((line = reader.readLine()) != null) {
    //process each line in some way
    if(line.contains("001") {
     fileWriter1.write(line);  
    } else if (line.contains("002") ) {
     fileWriter2.write(line);    
    }
  } 

【讨论】:

    【解决方案3】:

    代码完成。

     /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package jfile;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.net.URL;
    
    /**
     *
     * @author andy
     */
    public class JFile {
    
        /**
         * @param args the command line arguments
         */
    
        static File master = null,
                    m1     = null, // file output with "001"
                    m2     = null; // file output with "002"
    
        static FileWriter fw1,
                          fw2;
    
        static FileReader fr = null;
        static BufferedReader br = null;
    
        public static void main(String[] args) throws FileNotFoundException, IOException
        {
    
             String root = System.getProperty("user.dir") + "/src/files/";
    
             master = new File ( root + "master.txt" );
             m1     = new File ( root + "m1.txt");
             m2     = new File ( root + "m2.txt");
    
             fw1     = new FileWriter(m1, true);
             fw2     = new FileWriter(m2, true);
    
    
             fr = new FileReader (master);
             br = new BufferedReader(fr);
    
             String line;
             while((line = br.readLine())!=null)
             {
                 if(line.contains("001")) 
                 {               
                     fw1.write(line + "\n");
    
                 } else if (line.contains("002"))
                 {
                     fw2.write(line + "\n");
    
                 }
             }
    
             fw1.close();
             fw2.close();
             br.close();
        }
    
    }
    

    Netbeans 项目http://www.mediafire.com/?yjdtxj2gh785cyd

    【讨论】:

    • 如果一行包含 001 和 002 怎么办? (删除 if 之间的 else)
    猜你喜欢
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    相关资源
    最近更新 更多