Solved

Replace string in text java

Posted on 2014-09-22
6
247 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 132
what is a "java.lang.System Property"   ? 20 57
VB Script to add site to Java Exception List 4 49
Spring Framework HTTPSession management 1 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now