简体   繁体   English

在文件中查找单词并替换文件中新单词的方法(Java)

[英]Method to find a word in a file and replace a new words in a file(Java)

It couldnt replace the new word and place it in a new file.它无法替换新词并将其放入新文件中。

I want to create a method that take 4 parameters, one with oldfile, one with new file, one with old word and one with new word and they are all of type string.我想创建一个带有 4 个参数的方法,一个是旧文件,一个是新文件,一个是旧词,一个是新词,它们都是字符串类型。

I also want to make it so that he case of the first letter the oldWord should be maintained when writing to the in the newFile, eg if oldWord was “Hit” and newWord was “Cab” then if “Hit” is found in the oldFile then “Cab” should be written to the newFile.我还想这样做,以便在写入 newFile 时保留 oldWord 的第一个字母的大小写,例如,如果 oldWord 是“Hit”而 newWord 是“Cab”,那么如果在 oldFile 中找到“Hit”那么“Cab”应该被写入新文件。

Im not allowed to use advanced java stuff like hashkeys and all that.我不允许使用高级 java 之类的东西,比如哈希键等等。 Hope that enough infomaton and thank you in advance.希望有足够的信息并提前谢谢你。

My code couldnt print the new words into the new file instead it just prints 4 more lines of the new words in the old file.我的代码无法将新词打印到新文件中,而只是在旧文件中再打印 4 行新词。

////// //////

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class part2d {

    public static void main(String[] args)
        {
            modifyFile("test.txt","modify.txt", "Hit", "Cab");
            System.out.println("done");
        }

static void modifyFile(String oldfile, String newfile, String oldString, String newString)
{
    File fileToBeModified = new File("modify.txt");
    String oldContent = "";         
    BufferedReader reader = null;         
    FileWriter writer = null;
     
    try
    {
        reader = new BufferedReader(new FileReader(fileToBeModified));                     
        String line = reader.readLine();
         
        while (line != null) 
        {
            oldContent = oldContent + line + System.lineSeparator();                 
            line = reader.readLine();
        }
         
        String newContent = oldContent.replaceAll(oldString, newString);
         
        writer = new FileWriter(fileToBeModified,true);             
        writer.write(newContent);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {                 
            reader.close();                 
            writer.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();            
        }
    }

Both your reader and your writer are using the fileToBeModified variable.您的读者和作者都在使用fileToBeModified变量。 This variable is being set to modify.txt statically for both, so you're not actually reading and writing a new file, instead you're reading then appending the same file content again.此变量被静态设置为modify.txt ,因此您实际上并不是在读取和写入新文件,而是读取然后再次附加相同的文件内容。

Think about what file you're creating using the BufferedReader/FileReader and the FileWriter, and consider how these are being set.考虑一下您正在使用 BufferedReader/FileReader 和 FileWriter 创建什么文件,并考虑如何设置它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM