读取,扫描,修改文本文件并将修改后的文本放入新文件中

时间:2014-05-28 23:22:02

标签: java .net string text

我尝试读取文本文件并尝试修改它。我从StackOverflow那里得到了很多讨论,这里是内容:

  

NO 1025 0

     

NO 1025 1

     

OP 1026 0

     

EndRow

我想要的修改后的文本文件:

  

否0

     

AND 1

     

OP 0

     

EndRow

我阅读了一些关于它的讨论主题,然后得出结论我必须使用.hasNextLine方法来检查每一行。这是代码:

import java.io.*;
import java.util.Scanner;

public class MainConvert {

/**
 * @nahdiya
 */
public static void main(String[] args) {
    try {
        File readNet = new File("testttt.net");
        FileReader readFileNet = new FileReader(readNet);
        BufferedReader reader = new BufferedReader(readFileNet);
        Scanner scan = new Scanner("testttt.net");
        PrintWriter fileConvert = new PrintWriter("convertNet.txt");
        while (scan.hasNextLine()) {
            String check = scan.next();
            String checkLine = scan.nextLine();
            if (checkLine.contains("NO 1025")) {
                if(checkLine.contains("NO 1025")) {
                    fileConvert.println("AND "+check );
                } else if (checkLine.contains("OP 1026")) {
                    fileConvert.println("OP"+check);
                } else {
                    fileConvert.println(checkLine);}
                }
            }
        }
        reader.close();
        fileConvert.close();
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

当我尝试运行该类时,输出消息如下所示:

java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at fileConvertSave.MainConvert.main(MainConvert.java:21)

问题是:

PrintWriter fileConvert = new PrintWriter("convertNet.txt");

这条线有什么问题?我只想修改testttt.net文件。必须将fileConvert创建为新文件。这有什么问题?

2 个答案:

答案 0 :(得分:2)

已编辑:请参阅底部的完整解决方案:

产生错误消息的原始问题是Scanner尝试在不存在的行上执行nextLine(),因为:

String check = scan.next(); 
String checkLine = scan.nextLine(); 

致电时:

while( scan.hasNextLine() ) 

有下一行可用。然后你打电话:

scan.next();

此时可能没有“下一行”可用。然后你打电话:

scan.nextLine() 

和繁荣。

删除该行

String check = scan.next();

应该有效。

修改

这是问题所有其他部分的解决方案... 它基本上是对你所拥有的内容的完全重写,所以请阅读所有代码,了解它的作用并尝试了解这一切!如有疑问,请在提问前先阅读文档。

BufferedReader reader = null;
PrintWriter writer = null;

try {                                    
    reader = new BufferedReader(new FileReader("testttt.txt"));                       
    writer = new PrintWriter("convertNet.txt");            

    // setup the pattern that we want to find and replace in the input:
    // NB> this is a regex (or regular expression)
    // it means, find [space] then either 1025 or 1026 then [space]
    String patternToMatch = " (1025|1026) ";

    String inputLine = null;

    // while there are lines to read, read them one at a time:
    while ((inputLine = reader.readLine()) != null) {

        // create the outputLine which is the input line with our pattern
        // " 1025 " or " 1026 "  replaced by just a single space:
        String outputLine = inputLine.replaceFirst(patternToMatch, " ");

        // log the transformation for debugging purposes:                               
        System.out.println(inputLine + " -> " + outputLine);

        // write the outputLine to the output file:
        writer.println(outputLine);
    }
}  
catch (FileNotFoundException ex) {
    System.out.println("file was not found: " + ex);        
} 
catch (IOException ex ) {
    System.out.println("io error: " + ex);
}
finally {
    try {
        if( reader != null ) reader.close();
        if ( writer != null ) writer.close();
    } catch (IOException ex) {
        System.out.println("error closing file " + ex);
    }
}  

请注意,即使存在异常,finally块也会很好地清理。还有一种更新的方法可以使代码缩短,称为try with resources:

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

答案 1 :(得分:0)

你的字符串看起来不一致我建议你使用正则表达式,如果有更多这样的字符串和Bufferreader读取行,虽然我没有使用正则表达式,但这就是我提出的,

public static void main(String[] args) {
        try {
            File readNet = new File("testttt.net");
            FileReader readFileNet = new FileReader(readNet);
            BufferedReader reader = new BufferedReader(readFileNet);

            PrintWriter fileConvert = new PrintWriter("convertNet.txt");
            String r = null;
            while ((r = reader.readLine()) != null) {
                System.out.println(r);
                if (r.equals("NO 1025 1")) {
                    fileConvert.println(r.replace(r, "AND 1"));

                } else if (r.contains("1025 0")) {
                    fileConvert.println(r.replaceAll("1025", ""));
                } else if (r.contains("1026")) {
                    fileConvert.println(r.replaceAll("1026", ""));
                } else {
                    fileConvert.println(r);
                }
            }

            reader.close();
            fileConvert.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

祝你好运,我希望它可以帮到你。