Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6547413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:49:41+00:00 2026-05-25T11:49:41+00:00

I’m doing some relatively simple I/O in Java. I have a .txt files that

  • 0

I’m doing some relatively simple I/O in Java. I have a .txt files that I’m reading from using a Scanner and a .txt file I’m writing to using a BufferedWriter. Another Scanner then reads that file and another BufferedWriter then creates another .txt file. I’ve provided the code below just in case, but I don’t know if it will help too much, as I don’t think the code is the issue here. The code compiles without any errors, but it’s not doing what I expect it to. For some reason, charReader will only read about half of its file, then hasNext() will return false, even though the end of the file hasn’t been reached. These aren’t big text files – statsReader’s file is 34 KB and charReader’s file is 29 KB, which is even weirder, because statsReader reads its entire file fine, and it’s bigger! Also, I do have that code surrounded in a try/catch, I just didn’t include it.

From what I’ve looked up online, this may happen with very large files, but these are quite small, so I’m pretty lost.

My OS is Windows 7 64-bit.

        Scanner statsReader = new Scanner(statsFile);
        BufferedWriter statsWriter = new BufferedWriter(new FileWriter(outputFile));

        while (statsReader.hasNext()) {
            statsWriter.write(statsReader.next());
            name = statsReader.nextLine();
            temp = statsReader.nextLine();
            if (temp.contains("form")) {
                name += " " + temp;
                temp = statsReader.next();
            }
            statsWriter.write(name);
            statsWriter.newLine();
            statsWriter.write(temp);
            if (! (temp = statsReader.next()).equals("-"))
                statsWriter.write("/" + temp);
            statsWriter.write("\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "");
            statsWriter.newLine();
            statsReader.nextInt();
        }

        Scanner charReader = new Scanner(charFile);
        BufferedWriter codeWriter = new BufferedWriter(new FileWriter(codeFile));   

        while (charReader.hasNext()) {
            color = charReader.next();
            name = charReader.nextLine();
            name = name.replaceAll("\t", "");
            typing = pokeReader.next();
            place = charReader.nextInt();
            area = charReader.nextInt();
            def = charReader.nextInt();
            shape = charReader.nextInt();
            size = charReader.nextInt();
            spe = charReader.nextInt();

            index = typing.indexOf('/');
            if (index == -1) {
                typeOne = determineType(typing);
                typeTwo = '0';
            }
            else {
                typeOne = determineType(typing.substring(0, index));
                typeTwo = determineType(typing.substring(index+1, typing.length()));
            }
        }

SSCCE:

public class Tester {
public static void main(String[] args) {
    File statsFile = new File("stats.txt");
    File testFile = new File("test.txt");
    try {
        Scanner statsReader = new Scanner(statsFile);
        BufferedWriter statsWriter = new BufferedWriter(new FileWriter(testFile));
        while (statsReader.hasNext()) {
            statsWriter.write(statsReader.nextLine());
            statsWriter.newLine();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:49:41+00:00Added an answer on May 25, 2026 at 11:49 am

    This is a classic problem: You need to flush and close the output stream (in this case statsWriter) before reading the file.

    Being buffered, it doesn’t actually write to the file with ever call to write. Calling flush forces it to complete any pending write operations.

    Here’s the javadoc for OutputStream.flush():

    Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.