Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/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
为什么赢了';t BufferedWriter是否将URL内容写入文本文件?_Bufferedwriter - Fatal编程技术网

为什么赢了';t BufferedWriter是否将URL内容写入文本文件?

为什么赢了';t BufferedWriter是否将URL内容写入文本文件?,bufferedwriter,Bufferedwriter,我试图将URL中的文本以35行为一批写入文本文件,按enter键继续下一批35行。如果我不尝试以35行为一批写入该文件,它将非常有效,并将所有内容写入文本文件。但是,当我尝试使用if语句以35次为一批打印时,除非我按enter键15次左右,否则它不会打印到文件中。即使这样,它也不会打印所有内容。我似乎和if语句有关,但我搞不清楚 String urlString = "https://www.gutenberg.org/files/46768/46768-0.txt"; try {

我试图将URL中的文本以35行为一批写入文本文件,按enter键继续下一批35行。如果我不尝试以35行为一批写入该文件,它将非常有效,并将所有内容写入文本文件。但是,当我尝试使用if语句以35次为一批打印时,除非我按enter键15次左右,否则它不会打印到文件中。即使这样,它也不会打印所有内容。我似乎和if语句有关,但我搞不清楚

String urlString = "https://www.gutenberg.org/files/46768/46768-0.txt";

    try {
        URL url = new URL(urlString);
        try(Scanner input = new Scanner(System.in);
            InputStream stream = url.openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\mattj\\Documents\\JuliusCeasar.txt"));) {

            String line;
            int PAGE_LENGTH = 35;
            int lineCount = 0;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                writer.write(line + "\n");
                lineCount++;
                if (lineCount == PAGE_LENGTH){
                    System.out.println();
                    System.out.println("- - - Press enter to continue - - -");
                    input.nextLine();
                    lineCount = 0;
                }
            }
        }
    } catch (MalformedURLException e) {
        System.out.println("We encountered a problem regarding the following URL:\n"
                + urlString + "\nEither no legal protocol could be found or the "
                + "string could not be parsed.");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Attempting to open a stream from the following URL:\n"
                + urlString + "\ncaused a problem.");
        e.printStackTrace();
    }

我不懂Java,但在.NET中有非常相似的概念。我想这里有两件事要考虑。
BufferWriter
不会立即写入文件,顾名思义,它充当缓冲区,随着时间的推移收集写入请求,然后批量执行
BufferWriter
有一个
flush
方法可以立即刷新对文件的“排队”写入-因此,当您达到35次时,我会这样做(不要每次写入都刷新)


另外,
BufferedReader
BufferedWriter
是可关闭的,因此请确保将它们包装在
try
语句中,以确保资源被正确解锁/清除。

我不懂Java,但.NET中有非常类似的概念。我想这里有两件事要考虑。
BufferWriter
不会立即写入文件,顾名思义,它充当缓冲区,随着时间的推移收集写入请求,然后批量执行
BufferWriter
有一个
flush
方法可以立即刷新对文件的“排队”写入-因此,当您达到35次时,我会这样做(不要每次写入都刷新)

另外,
BufferedReader
BufferedWriter
是可关闭的,因此请确保将它们包装在
try
语句中,以确保资源正确解锁/清除