Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Replace string in text java

Posted on 2014-09-22
6
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

664 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