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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:00:44+00:00 2026-05-30T06:00:44+00:00

I am trying to build a servlet that parses form input and creates a

  • 0

I am trying to build a servlet that parses form input and creates a .csv file from them, but the bufferedWriter object truncates a lot of characters for no (apparent to me) reason.

        String filepath = getServletContext().getRealPath("\\") + "temp";
        String filename = "csv"+dateFormat.format(date)+".csv";
        File file = new File(filepath + filename);            
        file.createNewFile();
        BufferedWriter fwrite = new BufferedWriter(new FileWriter(file));
        for(int i=0; i<list.size(); i++) {
            String[] dataEntry = list.get(i);
            for (int j=0; j<dataEntry.length;j++)                 
                 fwrite.write("test1-2");
                //fwrite.append(dataEntry[j]+";");     
            fwrite.newLine();
        }
        fwrite.close();            
         URI fileUri = file.toURI();
         stream = response.getOutputStream();
         response.setContentType("text/csv");
         response.addHeader("Content-Disposition", "attachment; filename="
            + filename);

         URLConnection urlConn = fileUri.toURL().openConnection();
         response.setContentLength((int) urlConn.getContentLength());
         buf = new  BufferedInputStream(urlConn.getInputStream());                          
         while (buf.read() != -1)
            stream.write(buf.read());
        } finally {
        if (stream != null)
            stream.close();
        if (buf != null)
            buf.close();     
        }          
}

Sorry if the code is a bit slapdash. My current output when writing the “test1-2” string for each entry is

et-ts12et-ts12et-ts12ÿ

any further comments on the code itself would be appreciated I’m just experimenting with stuff i find on the net, I have no actual best practices points of reference.

  • 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-30T06:00:45+00:00Added an answer on May 30, 2026 at 6:00 am

    I’d maybe add a few methods so that no individual method is overly large. For example:

    /**
     * Saves the List of String[] to the File.
     * 
     * @param f
     * @param list
     * 
     * @throws IOException
     */
    void saveList(File f, List<String[]> list) throws IOException {
        FileWriter fw = null;
        try {
            fw = new FileWriter(f);
            saveList(fw, list);
        } finally {
            if (null != fw) {
                // Ensure that fw is closed.
                fw.close();
            }
        }
    }
    
    /**
     * Saves the List of String[] to the Writer.
     * 
     * @param w
     * @param list
     * 
     * @throws IOException
     */
    void saveList(Writer w, List<String[]> list) throws IOException {
        BufferedWriter bw = new BufferedWriter(w);
        for (int i = 0; i < list.size(); i++) {
            String[] dataEntry = list.get(i);
            for (int j = 0; j < dataEntry.length; j++) {
                bw.write("test1-2");
                // bw.append(dataEntry[j]+";");
            }
            bw.newLine();
        }
        bw.flush();
    }
    
    /**
     * Copies in's contents to out.
     * 
     * @param in
     *            Must not be null.
     * @param out
     *            Must not be null.
     * 
     * @throws IOException
     */
    void copyStream(InputStream in, OutputStream out) throws IOException {
        if (null == in) {
            throw new NullPointerException("in must not be null");
        }
        if (null == out) {
            throw new NullPointerException("out must not be null");
        }
        byte[] buf = new byte[1024 * 8];
        int read = -1;
        while ((read = in.read(buf)) > -1) {
            out.write(buf, 0, read);
        }
    }
    
    /**
     * Copies in's contents to out, and ensures that in is closed afterwards.
     * 
     * @param in
     *            Must not be null.
     * @param out
     *            Must not be null.
     * 
     * @throws IOException
     */
    void copyStreamAndCloseIn(InputStream in, OutputStream out) throws IOException {
        try {
            copyStream(in, out);
        } finally {
            in.close();
        }
    }
    
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filepath = getServletContext().getRealPath("\\") + "temp";
        String filename = "csv" + dateFormat.format(date) + ".csv";
        File file = new File(filepath + filename);
        file.createNewFile();
    
        saveList(file, list);
    
        long length = file.length();
        response.setContentType("text/csv");
        response.addHeader("Content-Disposition", "attachment; filename=" + filename);
        response.setContentLength((int) length);
    
        copyStreamAndCloseIn(new FileInputStream(file), response.getOutputStream());
    }
    

    As for the strange output et-ts12et-ts12et-ts12ÿ, I’m not sure why that would be.

    How are you viewing this value? Printing to the console, reading the file afterwards? Both printing to the console and opening the file in another editor could produce strange results depending on the character encoding in use.

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

Sidebar

Related Questions

I'm trying to build a basic servlet with scala, but I can't seem to
I'm trying build a method which returns the shortest path from one node to
I'm trying to configure build.xml files for build forge, but it seems like I
When trying to pass a table built with HTML in my servlet like that:
I am trying to build the mvc-showcase example available here link . But i
I am trying to build a sample application using ANT build. But i am
Hi All: I'm trying to set up my build file to be run for
I'm trying to setup a servlet so that users on Android handsets/tablets (using the
I am trying to build a web interface for an embedded device, that will
I'm trying to build a client jar file to access a webservice. I'm including

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.