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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:43:05+00:00 2026-06-10T06:43:05+00:00

I really not getting where the problem is. I wanted to print the characters

  • 0

I really not getting where the problem is.
I wanted to print the characters to a text file and im uisng printWriter to do the same.
if the file has a “;”, i want to replace it with a new line and this is what im doing,

public static void downloadFile_txt(String sourceFilePathName, String contentType, String destFileName, HttpServletResponse response) throws IOException, ServletException
 {
     File file = new File(sourceFilePathName);
     //FileInputStream fileIn = new FileInputStream(file);
     FileReader fileIn = new FileReader(file); 
     long fileLen = file.length();
     response.setContentType(contentType);
     response.setContentLength((int)fileLen);

     response.setHeader("Content-Disposition", String.valueOf((new  StringBuffer("attachment;")).append("filename=").append(destFileName)));

     PrintWriter pw =  response.getWriter();


     // Loop to read and write bytes.
     int c=-1;      
     while ((c = fileIn.read()) != -1)
     {

         if(c!=59)
         {
              pw.print((char)c);
         }
         else
         {
              pw.println();
         }
     }
     pw.flush();
     pw=null;        
     fileIn.close();        
}

But my file is priting everything except for the last character.
Eg.input =

:00004000,FFAD,2 Byte Ch;
:0000FFBD,FFBE,2 Byte Ch;
:0000FFBF,FFFF,2 Byte Ch;

output which im getting

:00004000,FFAD,2 Byte Ch
:0000FFBD,FFBE,2 Byte Ch
:0000FFBF,FFFF,2 Byte C

the last “h” is not getting printed.

Thanks in advance

  • 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-10T06:43:07+00:00Added an answer on June 10, 2026 at 6:43 am

    A pw.flush(); might help you.

    public class FlushPrintWriter {
    
        public static void main(String[] args) throws IOException  {
            FileReader fileIn = new FileReader("in.txt");
            FileWriter out = new FileWriter("out.txt");
            PrintWriter pw = new PrintWriter(out);
            int c;        
            while ((c = fileIn.read()) != -1) {
                if(c!=59) {
                   pw.print((char)c);
                } else {
                   pw.println();
                }
            }
            pw.flush();
        }
    }
    

    outputs

    :00004000,FFAD,2 Byte Ch
    
    :0000FFBD,FFBE,2 Byte Ch
    
    :0000FFBF,FFFF,2 Byte Ch
    
    

    as expected.

    (Don’t handle your IOExceptions like this – and close your readers and writers – this is for demonstration only!)


    edit: now your code doesn’t even compile (two vars called fileIn?)!

    Even when run through the servlet code you’re now mentioning, I can’t reproduce your problem, and the output is as you would expect. So this is me giving up. I’m starting to suspect either the final ; isn’t in your source file, or there is yet more processing your app is doing that you’re not showing us.

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

Sidebar

Related Questions

Really simple but just not getting it. I want to reference a view by
So i have a really weird problem with a knockoutjs click binding not getting
I'm not really getting how this code does what it does: char shellcode[] =
I feel like this might be really simple but I'm just not getting it
I'm not sure if I'm missing something really obvious, but I keep getting a
I'm still not really getting the upside of partial methods. Can anyone illustrate a
I wanted to post this because I was not really sure what issue I
I am really amazed about this location aware system in android and not getting
i am facing problem with the return type for this function and not really
I'm trying to use Ajax in CakePHP, and not really getting anywhere! I have

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.