I write to a .txt file a specific value that contains the time at which it was received. Here is the code (the code is ugly - these are only attempts to solve the necessary problem):

Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(txtText); int start = 0; int []rez = {0,0,0,0}; while (matcher.find(start)) { String value = txtText.substring(matcher.start(), matcher.end()); int result = Integer.parseInt(value); rez[start] = result; start = matcher.end(); } if(rez[3]-100 < 0){ rez[0] = rez[0] * 10; if(rez[3]-10 < 0){ rez[0] = rez[0] * 10; } } System.out.println(rez[0]+" "+rez[3]); DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); FileWriter writeFile = null; try { writeFile = new FileWriter(txtFile,true); writeFile.append(dateFormat.format(date)+"\t"); int foo = Integer.parseInt(rez[0]+""+rez[3]); writeFile.append(foo+"\n"); } catch (IOException e) { e.printStackTrace(); } finally { if(writeFile != null) { try { writeFile.close(); } catch (IOException e) { e.printStackTrace(); } } } 

The recorded file looks like this:

text file

But during the subsequent import all the data goes in one piece, i.e. I can not get a specific row, or a specific cell (the intersection of a row and column). I understand that it is a matter of writing data to a file. How can I write data to a file so that it can be divided again?

UPD:

The thing is, I'm trying to import into Wolfram Mathematica . I tried to write not to .txt, but to .xls ( Wolfram Mathematica normally imports it) using Apache POI , but I could not write to the .xls file the dynamically received data: I use the write() method, but the append() method in the Apache POI library no. I'll try to deal with CSV - most likely, this is what I need (I already use TSV ).

UPD2:

The CSV option does not work: I use TSV , and the import still fails.

  • 2
    "With subsequent imports" where? - awesoon
  • No matter. Wherever to import, it still goes in one piece - Yuri
  • one
    Important. Where you import, expects a certain format. - VladD
  • If you have found a solution - issue it with an answer, but do not answer directly in the question. - pavlofff

3 answers 3

It all depends on what format the application expects, where this data is imported. For example, you can write data in CSV format.

Sample input

 Display Name Age Micheal 30 Bill 25 

A comma is put between the cells to go to the next line \n

 FileWriter writer = new FileWriter(sFileName); writer.append("DisplayName"); writer.append(','); writer.append("Age"); writer.append('\n'); writer.append("Micheal"); writer.append(','); writer.append("30"); writer.append('\n'); writer.append("Bill"); writer.append(','); writer.append("25"); writer.append('\n'); writer.flush(); writer.close(); 

This file can already be imported into Excel, it will be a normal table.

  • one
    Yeah, and who will process the commas in the rows? And line feed characters in them? You write an unfamiliar format - do not write with your hands, but with a specialized library. - VladD
  • one
    Sign: to write hands CSV - to debag. - Nofate
  • It was an elementary example. The author writes data in a specific format, he has no commas. You make comments, look at it yourself first. When importing in Excel, it is possible to specify which characters are delimiters. - Sapphiron

I understand that you are importing into your own application and you can choose the data structure in the file, otherwise you do not have any special options and you need to make a file in a format that the import program understands.

To decide on data import between your applications, you need to use not just writing into one "heap" into a text file, but using some sort of data structuring language that has ready parsers and vrater in Java, which can be written and then parsed to get separate records , using either Java’s own libraries, or perhaps external ones, although in the case of such a simple structure, as in your case, they are not very necessary: ​​JSON, XML, CSV, and so on.

You can, of course, write your parser for such a structure as you have now (or use the pseudo-CSV format - add separators and markers that your parser understands), but this will not be a very logical decision, since there are ready-made debugged and documented solutions. .

    I solved the problem like this: I wrote it in a file in the form in which it would be easier for me later to work with this data in Wolfram Mathematika. Here is an example:

    {2015.08,06,04,13,43,445280}, {2015,0.08,06,04,13,53,445280}, {2015,0,0,0,0,0,04,04,445280},

    UPD
    The issue is resolved) Wolfram understands and CSV and TSV just had to show that it was CSV or TSV with a special function. There are still a lot of all formats.

    • If you find a solution - share it with the community, describe it in detail. What a special feature and so on. This will help others solve a similar problem. - pavlofff