Solved

Replace string in text java

Posted on 2014-09-22
6
245 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

14 Experts available now in Live!

Get 1:1 Help Now