Home java reading CSV in java

reading CSV in java

Author

Date

Category

Good evening. You need to read CSV files in which such content is Nr:
This means that at a certain time -time 2014-04-24 12:35:00 Metric TR_1 has a value of 120. The TR_2 metric has such a value of 55 and the TR_3 metric has such a value of 890 (data in scatter)

time; tr_1; TR_2; TR_3; VALUE
2014-04-24 12: 35: 00; 120; 55; 890
2014-04-24 12: 40: 00; 86; 555; 689
2014-04-24 12: 45: 00; 445; 7866; 235
etc ..

wrote code that displays only content simply:

Public Static Void Main (String [] Args) {
Data_TO_LOAD D = new data_to_load ();
D.Run ();
}
Public void Run () {
String csvfile = "\\ hh.csv";
BufferedReader br = null;
String Line = "";
String CVSSPLITBY = ";";
Try {
  Br = New BufferedReader (New Fileader (CSVFile));
  While ((Line = Br.ReadLine ())! = NULL) {
    STRING [] ROW = LINE.Split (CVSSPLITBY);
    System.out.PrintLN (
        ROW [0]
        + "" + ROW [1]
        + "" + ROW [2]);
  }
  } Catch (FileNotFoundException E) {
  E.PrintStackTrace ();
} Catch (IoException E) {
  E.PrintStackTrace ();
} finally {
  if (br! = null) {
    Try {
      Br.Close ();
    } Catch (IoException E) {
      E.PrintStackTrace ();
    }
  }

I can not understand how to do, so that display the contents of the files when reading in this form:

Time Name Value
2014-04-24 12:35:00 TR_1 120
2014-04-24 12:35:00 TR_2 55
2014-04-24 12:35:00 TR_3 890
2014-04-24 12:40:00 TR_1 86
2014-04-24 12:40:00 TR_2 555
2014-04-24 12:40:00 TR_3 689
2014-04-24 12:45:00 TR_1 445
2014-04-24 12:45:00 TR_2 7866
2014-04-24 12:45:00 TR_3 235

ie, the data in the CSV file is found in the form, i.e. TR_1 metrics. It is not found in the column and in the string. How to make TR_1 Metrics. In the line were issued to a column called Name and the values ​​of these metrics in the column called Value and orderly, i.e., D during 2014-04-24 12:35:00 TR_1 metric is 120 and other values ​​of the rest of the metrics in it same time Nr 2014-04-24 12:35:00 TR_2 metric is 55TD. Then another time interval 2014-04-24 12:40:00 and again, the TR_1 metric has a value of 86, the TR_2 metric has a value of 555 etc.. until the end of the file? Thank you

edit:

Made the following change:

list & lt; objekt & gt; Lines = New ArrayList & LT; Objekt & gt; ();
   String csvfile = "file.csv";
   BufferedReader br = null;
    String Line = "";
    String CVSSPLITBY = ";";
    Objekt Data_set = NULL;
     String [] Header = NULL;
     INT LINES_COUNT = 0;
     Br = New BufferedReader (New Fileader (CSVFile));
     While ((Line = Br.ReadLine ())! = NULL) {
       if (lines_count == 0) {
        Header = line.split (CVSSPLITBY);
     }
       ELSE {
        String [] temp = line.split (CVSSPLITBY);
        For (int i = 1; i & lt; temp.length; I ++) {
          data_set = new Objekt ();
          data_set.settimestamp (temp [0] + "\ t" + Header [i] + "\ t" + Temp [i] + "\ n");
              Lines.Add (Data_Set);
        }
        System.out.PrintLN (Lines);
        }
       Lines_COUNT ++;
     }
     Br.Close ();

I get at the output:

[10/06/2013 00:00 TR_1 100
06.10.2013 00:00 TR_2 250
06.10.2013 00:00 TR_3 25
, etc ...]

Advise, fire, how to change (i.e., exactly where it is in the code it is to change it) in order not to receive brackets and commas, but simply to output without them?

Edit: What needs to be done to get also the names of all files that are read in the Power Column, I tried it only to me the name of the last file in the folder, not all files.

public static list & lt; objekt & gt; Run () Throws IoException {
String Path2 = "D: \\ Folder \\ Files";
File Folder = New File (Path2);
File [] listOFFILES = folder.listfiles ();
For (int i = 0; i & lt; listoffiles.length; i ++)
{
  If (ListOffiles [i] .isfile ())
  {
    Files = ListOffiles [i] .getname ();
    if (files.endswith (". CSV"))
    {
      Files = files.replace (". CSV", "");
      System.out.printLN (Files);
    }
  }
}
List & lt; Objekt & gt; Lines = New ArrayList & LT; Objekt & gt; ();
String CSVString = "D: \\ Folder \\ Files \\ file1.csv";
BufferedReader br = null;
String Line = "";
String CVSSPLITBY = ";";
OBJEKT OBJEKT = NULL;
String [] HDR = NULL;
INT L_COUNT = 0;
Br = New BufferedReader (New FilerEader (CSVString));
While ((Line = Br.ReadLine ())! = NULL) {
  if (L_COUNT == 0) {
    HDR = line.split (CVSSSPLITBY);
  }
  ELSE.
    {
      String [] temp = line.split (CVSSPLITBY);
      For (int i = 0; i & lt; temp.length; i ++) {
        Objekt = New Objekt ();
        Objekt.SetTimeStamp (HDR [i] + "\ T" + Temp [0] + "\ t"
            + TEMP [i] + "\ t" + Files + "\ n");
        Lines.add (Objekt);
      }
      System.out.PrintLN (Lines);
    }
    L_COUNT ++;
  }
Br.Close ();
RETURN LINES;

This is how I gives me:

& gt; tr_klue 06.03.2014 11:30 1389 OutfileName
  & GT; TR_KLUE_LO 03/06/2014 12:00 1889 OUTFILENAME

Because files have different names, it is necessary that the name of each file is displayed, H-P:

& gt; tr_klue 06.03.2014 11:30 1389 OutfileName
  & GT; TR_KLUE_LO 03/06/2014 12:00 1889 Outfile1
  & gt; TR_KLUE 06.03.2014 12:30 100 PROPS2
  & GT; TR_KLUE_LO 03/06/2014 13:00 89 Colorak

Tell me a fire wherever I do wrong.


Answer 1, Authority 100%

string filename = "hh.csv";
  Try {
    BufferedReader FP = New BufferedReader (New FileReader (FileName));
    String [] HDR = FP.ReadLine (). Split (";");
    if (HDR! = NULL)
      System.out.PrintLN (HDR [0] + "\ t \ t \ t" + "Name \ t" + HDR [HDR.LENGTH - 1]);
    STRING [] COLS;
    While (FP.Ready ()) {
      cols = FP.ReadLine (). Split (";");
      for (int i = 1; i & lt; = 3; ++ i)
        System.Out.printLN (COLS [0] + '\ T' + HDR [i] + '\ T' + Cols [i]);
      COLS = NULL;
    }
    FP.Close ();
    FP = NULL;
    HDR = NULL;
  } Catch (Exception E) {
    E.PrintStackTrace ();
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions