• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Convert ASCII File to CSV File

 
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to convert an ASCII file to a CSV file. I found some code on the Internet to accomplish this, but it generated an error that I don't understand and can't rectify. I have a basic understanding of java. I'm using the Eclipse IDE. The file I couldn't attach was generated by a Leica LS10 digital level. The format is called field book format in the land surveying business. The purpose of converting the file is to import it into an Excel spreadsheet for processing.



I think these two lines are causing the issue.

       String inputFile = "D:\M_SOUTH ECON"; // Replace with your ASCII file path
       String outputFile = "D:\M_SOUTH ECON"; // Replace with desired CSV file path

Do I need to put extensions at the ends of the file names like "D:\M_SOUTH ECON.asc" and "D:\M_SOUTH ECON.txt"?

My error is

java.io.FileNotFoundException: D:\M_SOUTH ECON (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:159)
at java.base/java.util.Scanner.<init>(Scanner.java:662)
at AsciiToCsvConverter.main(AsciiToCsvConverter.java:11)
 
Bartender
Posts: 5584
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi  Milo,

welcome to the Ranch!

A '\' has a special meaning in a String, it 'escapes'the next character. If you want to use a '\', proceed it with another '\', so use: "D:\\M..".

What always works, on any OS, is to use the forward slash, '/'. So use "D:/M...", success guaranteed!
 
Master Rancher
Posts: 5112
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you get that source to compile?  When I try I get this error message:


TestCode28.java:243: error: illegal escape character



java.io.FileNotFoundException: D:\M_SOUTH ECON (The system cannot find the file specified)


Does that file exist?

Another problem I see is the output file having the same name and location as input file.  When FileWriter executes it will create a new empty file over the old file.

Do I need to put extensions at the ends of the file names like "D:\M_SOUTH ECON.asc" and "D:\M_SOUTH ECON.txt"?


Yes that would give the files unique names.
 
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to import the output into Excel then use ".csv" as the output file suffix.
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use ".asc" as the suffix for the ASCII file if you want but the typical suffix is ".txt" which is what most editors will be looking for.
 
Saloon Keeper
Posts: 28486
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helpful hint:

1. "final" is basically the same as "const" in languages such as C. The compiler will flag an error if you try to accidentally change its value. Feel free to use "final" for your file path declarations and other stuff that shouldn't change.
2. A convention for manifest constants is to make their name all uppercase characters. This was stolen from the C programming language and is common, though not universal in Java.
3. I changed the name to CSV_DELIMITER just to make it more obvious that it wasn't something like, say, a filename delimiter.

None of the above are mandatory, but they can make it easier to avoid forgetting or breaking things if you come back in 5 years and want to steal or improve the code.
 
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . "final" is basically the same as "const" in languages such as C. . . .

Is it? You can use const to fix the value of a pointer in C, and you can then use const again to fix the contents of the pointer. That is particularly useful to reassure the caller of a function that their values passed as arguments will not be changed. The keyword const does exist in Java®, but is never used and the JLS (=Java® Language Specification) has its own ideas why const (and goto) are still there
The Java® keyword final can be applied to a reference to make sure that the reference will not be changed; it does not however make a reference type quoted immutable or anything like that. I think the tenets of object‑oriented programming include the notion that the creator of a type decides whether it is mutable or immutable. If a reference type is made mutable, it remains mutable forever (and vice versa). I suspect other people will disagree with this notion.
If a Java® method declares a parameter as final, it means it won't be changed in that method, but the state of any reference type passed may be changed, and that change will also occur in the original. If the value of any parameter is changed in the method, that change will not be reflected to the calling code because Java® only uses call‑by‑value and can neither implement nor mimic call‑by‑reference.

None of the above are mandatory, but they . . .

. . . are really good ideas which I think, agreeing with Tim H, you should implement.
 
Tim Holloway
Saloon Keeper
Posts: 28486
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the word "basically" deliberately. final is not the same as const, as you have noted. And while it would be useful to have the ability to make a reference immutable (or an immutable refrence, in general!), there are some "gotchas" that I think have prevented that from being adopted.

My own theories about the "dead" keywords in Java are twofold:

A) Back when the language was being defined, Java adopters were primarily C/C++ programmers and some guards to prevent them from attempting to code "C" code in Java - and possibly succeeding in unexpected ways - were in order.

B) The language was still being formed, and they weren't certain about what features originally deemed unwanted/unnecessary might need to be added later. Making these keywords reserved would avoid possible future code breaking.

Ironically, if memory serves, though, "enum" was not originally a reserved word in Java. And, indeed, its addition did break some existing code.
 
Milo Hockinger
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Helpful hint:

Done.

 
Milo Hockinger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:
If you want to use a '\', proceed it with another '\', so use: "D:\\M..".

Done. I got the single backslash from right-clicking on the M_SOUTH ECON file and selecting properties.
Why is only one backslash shown there?

 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"D:\\M.." is a String constant which gets compiled to a String object and during compilation escape sequences starting with a backslash are converted into one or more characters but the leading backslash IS NOT copied to the final String in memory. So in this example only a single backslash actually ends up in memory after compilation.
 
Milo Hockinger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:
How did you get that source to compile?


I didn't.

java.io.FileNotFoundException: D:\M_SOUTH ECON (The system cannot find the file specified) Does that file exist?


Yes. I changed the file extension and path to D:\\M_SOUTH ECON.asc.

Another problem I see is the output file having the same name and location as input file.  When FileWriter executes it will create a new empty file over the old file.


The files are now named M_SOUTH ECON.asc and M_SOUTH ECON OUT.txt.
 
Marshal
Posts: 4699
588
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error messages are you seeing?

I tried what I believe is your code after your changes, and it compiled clean.

I ran it with D:\M_SOUTH ECON.asc containing this:
and got output to D:\M_SOUTH ECON OUT.txt containing this:
 
Milo Hockinger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:What error messages are you seeing?


Build path specifies execution environment JavaSE-22. There are no JREs installed in the workspace that are strictly compatible with this environment.

The console reads <terminated> AsciiToCsvConverter [Java Application] C:\Program Files\Java\jdk-23\bin\javaw.exe

This is the file I'm trying to convert.

-------------------------------------------------------------------------------------
Leica,Geosystems,AG,---,Digital,Level,Measurement,Report
-------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------
Job,name,:,SOUTH,ECON
-------------------------------------------------------------------------------------
Operator,:,MH
LS10,:,Instr.Nr.:,711598
Remarks,:,/
Date/Time,:,16.12.2024,/,22:39:17
-------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------
Line,Levelling
-------------------------------------------------------------------------------------


Line,:,LINE00001,Method:,BF
=====================================================================================
Date,:,16.12.2024,Time,:,22:55:29
Staff1:,Staff2:
=====================================================================================
,PointID,Backsight,Intmdt,Foresight,delta,H,Distance,Pt,Height
-------------------------------------------------------------------------------------
,Start,PtID
,H1716017,78.6140

,H1716017,5.6348,235.85
,1,3.4804,+2.1544,235.43,80.7684
,1,4.9514,154.80
,2,3.4930,+1.4584,155.45,82.2268
,2,4.8275,96.94
,3,3.9336,+0.8939,97.15,83.1207
,3,3.4254,80.31
,4,3.5648,-0.1394,80.80,82.9813
,4,3.4791,98.67
,5,4.4857,-1.0066,97.86,81.9747
,5,5.0588,100.44
,6,4.9653,+0.0935,97.31,82.0682
-------------------------------------------------------------------------------------

I was unable to attach the file to my post.
 
Ron McLeod
Marshal
Posts: 4699
588
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The content that you posted looks it has already been processed through something similar to the code you are working with (not only replacing white space with a comma, but also removing ¦ characters).  

Isn't this (below) the type of report that comes from the instrument?  If it is, then I don't think that the code is going to do what you are wanting.

-------------------------------------------------------------------------------------
Leica Geosystems AG --- Digital Level Measurement Report
-------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------
Job name   : 0226ED          
-------------------------------------------------------------------------------------
Operator   : --------        
DNA03      : Instr.Nr.: 343161    
Remarks    : --------         / --------        
Date/Time  : 26.02.2019       /  6:13:39
-------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------
Line Levelling
-------------------------------------------------------------------------------------

Line  : LINE00001        Method: BF        
=====================================================================================
Date  : 26.02.2019       Time  : 06:18:49
Staff1: --------         Staff2: --------        
=====================================================================================
¦     PointID    ¦Backsight ¦  Intmdt  ¦Foresight ¦ delta H  ¦ Distance ¦ Pt Height ¦
-------------------------------------------------------------------------------------
¦Start PtID      ¦          ¦          ¦          ¦          ¦          ¦           ¦
¦B26             ¦          ¦          ¦          ¦          ¦          ¦   582.7263¦
¦                ¦          ¦          ¦          ¦          ¦          ¦           ¦
¦B26             ¦   5.84220¦          ¦          ¦          ¦    124.24¦           ¦
¦1               ¦          ¦          ¦   3.45202¦  +2.39019¦    123.91¦   585.1165¦
¦1               ¦   5.99359¦          ¦          ¦          ¦     73.47¦           ¦
¦M2              ¦          ¦          ¦   2.41742¦  +3.57617¦    120.08¦   588.6927¦
¦M2              ¦   4.43532¦          ¦          ¦          ¦     44.83¦           ¦
¦              P1¦          ¦   3.82759¦          ¦  +0.60773¦     77.07¦   589.3004¦
¦              M1¦          ¦   2.43419¦          ¦  +2.00113¦    129.85¦   590.6938¦
¦2               ¦          ¦          ¦   4.18279¦  +0.25254¦    126.56¦   588.9452¦
¦2               ¦   6.29322¦          ¦          ¦          ¦     30.13¦           ¦
¦              M3¦          ¦   3.73870¦          ¦  +2.55452¦     39.23¦   591.4997¦
¦3               ¦          ¦          ¦   1.07662¦  +5.21660¦     64.17¦   594.1618¦
¦3               ¦   5.33935¦          ¦          ¦          ¦     34.29¦           ¦
¦              P2¦          ¦   2.82358¦          ¦  +2.51577¦     59.53¦   596.6776¦
¦4               ¦          ¦          ¦   2.07683¦  +3.26252¦     58.24¦   597.4243¦
¦4               ¦   6.33206¦          ¦          ¦          ¦     39.11¦           ¦
¦5               ¦          ¦          ¦   1.28898¦  +5.04308¦     68.21¦   602.4674¦
¦5               ¦   5.60334¦          ¦          ¦          ¦     32.78¦           ¦
¦              P3¦          ¦   2.10250¦          ¦  +3.50084¦     63.91¦   605.9682¦
¦6               ¦          ¦          ¦   1.64382¦  +3.95952¦     65.29¦   606.4269¦
¦6               ¦   5.83322¦          ¦          ¦          ¦     39.46¦           ¦
¦              M6¦          ¦   2.83305¦          ¦  +3.00017¦     44.78¦   609.4271¦
¦7               ¦          ¦          ¦   0.98366¦  +4.84957¦     55.94¦   611.2765¦
¦7               ¦   5.14413¦          ¦          ¦          ¦     34.30¦           ¦
¦8               ¦          ¦          ¦   2.47878¦  +2.66535¦     41.84¦   613.9418¦
¦8               ¦   6.23389¦          ¦          ¦          ¦    112.76¦           ¦
¦B30             ¦          ¦          ¦   3.20561¦  +3.02828¦    105.65¦   616.9701¦


Example report downloaded from RPLS.com
 
Milo Hockinger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:
Isn't this (below) the type of report that comes from the instrument?


Yes. That's exactly right. My goal is to write a program that can take lines B26 through B30, for this example, and create lines of comma delimited data.
Everything above line B26 can be deleted. The reason I want comma delimited data is so I can import the data into an Excel spreadsheet for processing.
Will a java program be able to do that, or would another programming language be a better choice?
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Ron McLeod
Marshal
Posts: 4699
588
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an alternative using NIO and Streams:
Output from the example file would look like this:
CSV file opened in Excel:
 
Tim Holloway
Saloon Keeper
Posts: 28486
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Milo Hockinger wrote:
Build path specifies execution environment JavaSE-22. There are no JREs installed in the workspace that are strictly compatible with this environment.


This error message is produced when you try to launch the latest release of Eclipse but Eclipse cannot find a Java 22 JDK. The problem lies in the eclipse.ini file where Eclipse is installed. You have to  set the JDK patch as a value in that file. I forget if eclipse.ini is in the Eclipse root directory (NOT your project directory!!!) or if it's in a subdirectory.

Setting JAVA_HOME doesn't work. Eclipse doesn't use JAVA_HOME.

If Eclipse launches but cannot build and you get a message like that then you'll have to use the Eclipse menu options to register your Java 22 JDK location with it.

Sorry I cannot be more specific, but the machine I'm at doesn't have Eclipse on it  it's just a Raspberry Pi.

***

As you've seen, backslashes are dangerous. As was mentioned earlier, a safer - and more portable - path value would be like so:
 
Milo Hockinger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Carey Brown

Thanks for the help, Carey. How would I tell my program to begin at the line proceeding
¦Start PtID and ending at the last line? The last line will never be the same because the
data will vary from job to job. ¦Start PtID will always be there because the measurement
report is formatted that way.

=====================================================================================
¦     PointID    ¦Backsight ¦  Intmdt  ¦Foresight ¦ delta H  ¦ Distance ¦ Pt Height ¦
-------------------------------------------------------------------------------------

¦Start PtID

¦B30             ¦          ¦          ¦   3.20561¦  +3.02828¦    105.65¦   616.9701¦
 
Piet Souris
Bartender
Posts: 5584
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean by 'proceeding' 'following'? Here is a third version, after Careys and Rons code:
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Do you mean by 'proceeding' 'following'? Here is a third version, after Careys and Rons code:



I think they might have meant "preceding" (i.e. before).
 
Let's get him boys! We'll make him read this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic