Results 1 to 9 of 9
  1. #1
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Java comma separated text file to csv

    I am cleaning up print logs from an old system to be used in excel. I can get them down to each value is separated by a comma and looks like this.

    TIME,1009 9/18/14,F/A RATIO,0.7590,NET AIR CNTS,66018.2,NET FLAG CNTS,50107.5,BKGROUND CNTS,61.0BW OF FLAG,49.807,DFRAC,-0.1834,ZFRAC,0.0000UP AIR TEMP,104.32,LO AIR TEMP,98.51,SOURCE TEMP,91.11,RCVR TEMP,97.95,,UP HEAD TEMP,93.89,LO HEAD TEMP,81.26
    TIME,1026 9/18/14,F/A RATIO,0.7589,NET AIR CNTS,66026.3,NET FLAG CNTS,50107.4,BKGROUND CNTS,61.0BW OF FLAG,49.829,DFRAC,-0.1660,ZFRAC,0.0000UP AIR TEMP,104.93,LO AIR TEMP,98.42,SOURCE TEMP,92.65,RCVR TEMP,99.49,,UP HEAD TEMP,94.82,LO HEAD TEMP,82.23
    TIME,1042 9/18/14,F/A RATIO,0.7584,NET AIR CNTS,66076.3,NET FLAG CNTS,50109.2,BKGROUND CNTS,61.0BW OF FLAG,49.955,DFRAC,-0.0652,ZFRAC,0.0000UP AIR TEMP,104.72,LO AIR TEMP,97.91,SOURCE TEMP,93.36,RCVR TEMP,99.52,,UP HEAD TEMP,95.31,LO HEAD TEMP,82.47

    where value follows its header. how can i export this to csv or excel with rows and columns?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java comma separated text file to csv

    What happens with that file when you open it in excel?
    If it doesn't work, what needs to be changed?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Java comma separated text file to csv

    A csv file isn't magical. In fact, the reason it's so widely uses is its simplicity.

    A csv file is just a bunch of Comma Separated Values. If you want rows and columns, then separate the columns by commas, and the rows by newlines.

    It sounds like maybe what you're trying to do is parse the headers out, print them as the first row, then remove them from the subsequent rows. What happens if you try that?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  4. #4
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Java comma separated text file to csv

    Quote Originally Posted by Norm View Post
    What happens with that file when you open it in excel?
    If it doesn't work, what needs to be changed?
    currently each item occupies a call.....
    |TIME|1009 9/18/14|F/A RATIO|0.7590|NET AIR CNTS|66018.2|NET FLAG CNTS|50107.5|

    i need them in columns to be graphed later.
    |TIME||F/A RATIO||NET AIR CNTS||NET FLAG CNTS||
    |1010 9/18/14|0.7591|76018.2|50103.5|
    |1011 9/18/14|0.7556|66718.2|50107.5|
    |1012 9/18/14|0.7590|67018.2|51107.5|
    |1013 9/18/14|0.7582|66017.2|50307.5|


    did my best to draw an excel sheet lol

  5. #5
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Java comma separated text file to csv

    Quote Originally Posted by KevinWorkman View Post
    A csv file isn't magical. In fact, the reason it's so widely uses is its simplicity.

    A csv file is just a bunch of Comma Separated Values. If you want rows and columns, then separate the columns by commas, and the rows by newlines.

    It sounds like maybe what you're trying to do is parse the headers out, print them as the first row, then remove them from the subsequent rows. What happens if you try that?
    ihave been trying that, not much luck....but to the best of my CSV knowledge, that exactly what they look like when opened in a text editor

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Java comma separated text file to csv

    How are you doing the parsing? Can you post an MCVE?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Java comma separated text file to csv

    Quote Originally Posted by KevinWorkman View Post
    How are you doing the parsing? Can you post an MCVE?
    this is what i have to get it to what you see above.

    Java Code:
    package OMC_Stage_Six;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /**
     *
     * @author leighlarue
     */
    public class OMC_Stage_Six {
    
        public static void main(String[] args) {
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("/Users/leighlarue/Desktop/Stage_Five.txt")));
                StringBuffer stringBuffer = new StringBuffer();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    line = line.replaceAll("UP HEAD TEMP", ",UP HEAD TEMP");
                    line = line.replaceAll("F/A RATIO", ",F/A RATIO");
                    line = line.replaceAll("TIME", "\nTIME");
                    line = line.replaceAll(",,", ",");
                    line = line.replaceAll("TIME,", "");
                    stringBuffer.append(line);
    
                }
                BufferedWriter bwr = new BufferedWriter(new FileWriter(new File("/Users/leighlarue/Desktop/Stage_Six.txt")));
    
                bwr.write(stringBuffer.toString());
    
                bwr.flush();
    
                bwr.close();
                System.out.println(stringBuffer.toString());
            } catch (IOException e) {
            }
        }
    
    }

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Java comma separated text file to csv

    It looks like all you're doing is adding commas. That's not enough, if you're trying to convert those headers to a header that only shows up at the top of the file.

    If you want the header to show up only at the top of the file, then you have to remove it from the "body" of the csv file, not just add commas.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  9. #9
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Java comma separated text file to csv

    Thank you, that is what i missing...adding the header to the top of the form...didn't think it would be as simple as you said it and made it click.

Similar Threads

  1. Reading a comma separated file into JTextPane
    By arndtmatt in forum New To Java
    Replies: 3
    Last Post: 04-09-2012, 11:08 PM
  2. MySQLDump to comma dilim text file
    By Sobutai in forum New To Java
    Replies: 1
    Last Post: 05-11-2011, 08:39 AM
  3. Delimite the file using comma
    By gokulcool in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 06:40 PM
  4. Replies: 7
    Last Post: 05-23-2008, 04:46 AM
  5. Replies: 0
    Last Post: 04-16-2008, 11:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •