Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting a carriage return and outputting to a file

Status
Not open for further replies.

maynard444

Programmer
Jul 1, 2003
13
0
0
US
Hi all,
I am lost on how to insert a carriage return after a ~ and export that to a file. I am pulling a file into a string buffer. At this point I would like to output to a file with carriage returns instead of one long string.The file is a text file (txt).
Here's what I have:
blah~blah*blah~blah*blah*blah*blah~

Here is what I want in the output file.
blah~
blah*blah~
blah*blah*blah*blah~

So basically I want to take the string and split it at the tildas, then output to a file with carriage returns at the end.
Any help that can be given is good.
 
public class Split {
public static void main(String[] args) throws Exception {
String line = "blah~blah*blah~blah*blah*blah*blah~";
for (int i = 0; i < line.length(); i++) {
char ch = line.charAt(i);
System.out.print(ch);
if (ch == '~') {
System.out.println();
}
}
}
}

Sean McEligot
 
This works well if I want to output this information to the console. I need to do the exact same thing this program does, except I need it to print out to a file in the exact same format. That is where I am having the problem.
 
here is what I have right now:

import java.io.*;
import java.util.*;

public class Split {
public static void main(String[] args) throws Exception {

StringBuffer fileAsStringBuffer = new StringBuffer();
BufferedReader input = new BufferedReader(new FileReader(&quot;text.txt&quot;));
String line;
while((line = input.readLine()) != null){
fileAsStringBuffer.append(line);
}
input.close();


for (int i = 0; i < fileAsStringBuffer.length(); i++) {
int firstIndex=i;
char ch = fileAsStringBuffer.charAt(i);
System.out.print(ch);

if (ch == '~') {
System.out.println();
}
}
}
}
 
Code:
FileWriter fout = new FileWriter(&quot;filename.txt&quot;);
PrintWriter out = new PrintWriter(fout);

...


    out.print(ch);
    if (ch == '~') {
        out.println();
      }

...

out.close();
[code]

Sean McEligot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top