Solved

Replace string in text java

Posted on 2014-09-22
6
251 Views
Last Modified: 2014-09-22
Hi:
I have a comma delimited text file and I want to replace one of the 'sections' with data from another.
I have written the process that collects the data as below but my problem is how to get the resulting string 'zz' into the proper place in the file (column 7 in the attached text file), replacing the existing text. Thus line 2 column 7 changes from
"20TKWIN/WIND FARM 00712" to "20-00712' as per array zz. (Yeah, line 1 is different - ignore that.)
Does anybody know how to do this?
Thanks!truemans.csv

Code to get the zz string :

    public static void main(String[] args) {
       
        /** amend file */
                 try {
             BufferedReader br = new BufferedReader(new FileReader("G:\\Sage50\\truemans.csv"));
            PrintStream psout = new PrintStream(new FileOutputStream("G:\\Sage50\\truemanshow.csv"));
                           String buff = null;
               while((buff=br.readLine()) != null){
                   String [] ss = buff.split(",");
                   String jobs = ss[6].substring(ss[6].length()-5);
                   String units = ss[6].substring(0,3);
                   String zz = units + "-" + jobs;
                    System.out.println(zz);
 
                              }

               br.close();
         

           } catch(Exception ex){
           }
0
Comment
Question by:ClaytonGlass
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 40336410
I would use a proper csv reader really but what's wrong with just writing the array you've derived from the line out to a new temp file after the manipulation?
0
 

Author Comment

by:ClaytonGlass
ID: 40336426
Thanks, CEHJ, I could write out the array to a new file - but it is parsing it into the correct 'column' that I am struggling with. I am guessing that I should create a series of arrays, each representing a column in the file, then slot  array 'zz' into the 7th point? Or is there another way? So build a file from a series of arrays?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40336484
Thanks, CEHJ, I could write out the array to a new file - but it is parsing it into the correct 'column' that I am struggling with.
You're not - you've done that quite successfully ;) And you've made the change successfully. Now you just need to write out the whole array again (which would be greatly simplified by using a csv writer)
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 86

Expert Comment

by:CEHJ
ID: 40336487
       } catch(Exception ex){
           } 

Open in new window

See http://technojeeves.com/index.php/aliasjava1/94-ignoring-exceptions-is-dangerous
0
 

Author Closing Comment

by:ClaytonGlass
ID: 40336492
Thank you very much CEHJ. Please excuse my newbieness! I don't know what you mean by a csv writer but java has done the job! Thanks again.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40336547
http://ostermiller.org/utils/javadoc/CSVPrint.html

is the kind of thing i meant

You can also try

ss[6] =  ss[6].replaceAll("(^\\d{2}).*(\\d{5})", "$1-$2");

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question