有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我尝试读取一个文本文件并尝试修改它。我从StackOverflow得到了很多讨论,以下是内容:

NO 1025 0

NO 1025 1

OP 1026 0

EndRow

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

NO 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) 个答案

  1. # 1 楼答案

    您的字符串看起来不一致,我建议您使用正则表达式,如果有更多像这样的字符串和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();
            }
    
        }
    

    祝你好运,我希望这对你有帮助

  2. # 2 楼答案

    编辑:查看底部的完整解决方案:

    产生错误消息的原始问题是扫描仪试图在不存在的行上执行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