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 8527321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:31:31+00:00 2026-06-11T08:31:31+00:00

I am reading a file’s contents and trying to print the contents using java.

  • 0

I am reading a file’s contents and trying to print the contents using java. But it prints junk characters along with the file content.

Code:

import java.io.*;

public class ReadFile {

    public String readFile(String filePath){

        StringBuilder contents = new StringBuilder();
        File file = new File(filePath);


        try{
            String lines = null;
            FileReader fileReader1 = new FileReader(file);
            BufferedReader buffer = new BufferedReader(fileReader1);

            while((lines = buffer.readLine())!=null){
                contents.append(lines);

            }
            buffer.close();
        }
        catch(FileNotFoundException ex){
                System.out.println("File not found.");
        }catch(IOException ex){
            System.out.println("Exception ocurred.");
        }
        return contents.toString();
    }

    public static void main(String[] args){

        ReadFile rf = new ReadFile();
        String lines = rf.readFile("C:\\Data\\FaultDn.txt");

        System.out.println("Original file contents: " + lines);


    }
}

The file contents are:

partner.cisco.com:org-root/mac-pool-QA_MAC_Pool_5-Sep-2012_12:00

The output is:

"Original file contents: ÿþp ..." and then junk characters after every letter.

Can you please point me to what I am missing in this code?

  • 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-11T08:31:32+00:00Added an answer on June 11, 2026 at 8:31 am

    Issue resolved. I have corrected my powershell script by adding ASCII encoding as follows:

    Out-File -Encoding ASCII 
    

    and I have used

    System.getProperty("line.separator");
    

    This resolves the issue and prints the file contents as it is (including new lines)

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

Sidebar

Related Questions

I'm reading file using java. File contains € chars but java interpret it like
Reading file using java and jcifs on windows. I need to determine size of
I am reading file contents in perl by following code my @files = glob($PATH/*);
I am using Java BufferedReader and reading file line by line using bufferedReader.readline(). Is
I am reading a file by character but I want to skip number of
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
I am reading .csv file through PHP using fgetcsv(). Now I am able to
I am reading one file using ruby in which I have written regexp in
I have a problem showing my progress bar when reading a file in Java.
I have some trouble reading file in Java. What a file looks like: Answer

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.