Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何读取文件并执行查找和替换?_Java_File_Io - Fatal编程技术网

Java 如何读取文件并执行查找和替换?

Java 如何读取文件并执行查找和替换?,java,file,io,Java,File,Io,我该如何阅读《哈克贝利·费恩全集》的文本文件,并用“a”替换出现的每一个“the”字 在课堂上想出了这个更简单的方法 import java.io.*; import java.util.*; public class Filey { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Scanner s = null; String

我该如何阅读《哈克贝利·费恩全集》的文本文件,并用“a”替换出现的每一个“the”字


在课堂上想出了这个更简单的方法

import java.io.*;
import java.util.*;
public class Filey {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Scanner s = null;
    String meAlong="";
    PrintWriter writer = null;

    try {
        s=new Scanner(new BufferedReader(new FileReader("HuckFinn.txt")));
        writer = new PrintWriter("output.txt");
        while (s.hasNext())
        {
            meAlong=s.nextLine();
            meAlong=meAlong.replace("the" , "a");
            meAlong=meAlong.replace("The", "A");
            writer.println(meAlong);
        }
    }
    finally 
    {
        if (s !=null)
        {
            s.close();
            writer.close();
        }
    }

}

}
import java.io.*;
import java.util.*;
public class Filey {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Scanner s = null;
    String meAlong="";
    PrintWriter writer = null;

    try {
        s=new Scanner(new BufferedReader(new FileReader("HuckFinn.txt")));
        writer = new PrintWriter("output.txt");
        while (s.hasNext())
        {
            meAlong=s.nextLine();
            meAlong=meAlong.replace("the" , "a");
            meAlong=meAlong.replace("The", "A");
            writer.println(meAlong);
        }
    }
    finally 
    {
        if (s !=null)
        {
            s.close();
            writer.close();
        }
    }

}

}