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

  • SEARCH
  • Home
  • 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 7963805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:42:06+00:00 2026-06-04T05:42:06+00:00

This is a code snippet but basically what I want to do is read

  • 0

This is a code snippet but basically what I want to do is read from a file named ‘listings.txt’ and write to a file named ‘overview.txt’. I want to take the information out of ‘listings.txt’ and put them into ‘overview.txt’ as is (I will figure out the rest later).

The file ‘overview.txt’ is created and appears to loop through the file ‘listings.txt’ and write to ‘overview.txt’. However, once I open the file ‘overview.txt’ it is empty.
Could someone go through a quick glance at my code and spot something erroneous?

package yesOverview;

import java.io.BufferedReader;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class yesOverview {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String strInput = "foo.bar";
        System.out.print("Please enter the listings file (the full path to the file): ");
        strInput = input.next();

        //This makes sure that the inputed file is listings.txt as required for KET1 task 2
        while (strInput.contains("listings.txt") == false) {
            System.out.print("Incorrect file. Please enter listings file(the full path to the file): ");
            strInput = input.next();
        }

        infos(strInput);
        input.close();
    }

    public static void infos(String strInput) {
        Scanner input2 = new Scanner(System.in);
        System.out.print("Please enter the overview.txt file (the full path to the file): ");
        String strInput2 = "foo.bar";
        strInput2 = input2.next();

        //This also makes sure that the overview.txt file is provided. 
        while (strInput2.contains("overview.txt") == false) {
            System.out.print("Incorrect file. Please enter overview file(the full path to the file): ");
            strInput2 = input2.next();
        }

        //Creates the file f then places it in the specified directory.
        File f = new File(strInput2);

        try {
            //Creates a printerwriter out that writes to the output file. 
            PrintWriter out = new PrintWriter(strInput2);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(KETTask2Overview.class.getName()).log(Level.SEVERE, null, ex);
        }

        //String that holds the value of the next line. 
        String inputLine = "";

        //Creates the Buffered file reader / writer.  
        try {
            BufferedReader in = new BufferedReader(new FileReader(strInput));
            FileWriter fstream = new FileWriter(strInput2);
            BufferedWriter out = new BufferedWriter(fstream);
            while (in.readLine() != null) {
                out.write(in.read());
            }
            in.close();
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }

    }
}
  • 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-06-04T05:42:07+00:00Added an answer on June 4, 2026 at 5:42 am

    Try this

    Close the BufferedWriter stream (ie out.close() )

    try and use nextLine() instead of next(), as next() only takes in a single word, but for a complete line use nextLine(), though this doesnt seem to be the problem here.

    What i do when i have to read and write to files, i normally follow these steps

    For Reading from a file

    File f = new File("my.txt");
    FileReader fr = new FileReader(f);
    BufferedReader br  = new BufferedReader(fr);
    
    String s = null;
    
    while ((br.readLine())!=null) {
    
    // Do whatever u want to do with the content of the file,eg print it on console using SysOut...etc
    
    }
    
    br.close();
    

    For Writing to a file:

    Boolean isDone = true;
    Scanner scan = new Scanner(System.in);
    File f = new File("my.txt");
    FileWriter fr = new FileWriter(f);
    BufferedWriter br  = new BufferedWriter(fr);
    
    while (isDone) {
    
       if (!isDone) {
    
     br.write(new Scanner(System.in).nextLine());
    
     }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

VS 2008 I have this code snippet I found on a VB website. But
I saw this code snippet const volatile int * volatile * const X; but
This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
This code basically fetches data from database and display it in a list along
I have this code snippet: <div id=div1> </div> <div id=div2> <h3>This is the content</h3>
A friend gave me this code snippet in Clojure (defn sum [coll acc] (if
I am using this code snippet to add KeyDown event handler to any element
I have this code snippet inside a function that checks if an object exists
I saw this code snippet during our lab and it actually compiles in MSVC2008

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.