• Post Reply Bookmark Topic Watch Topic
  • New Topic

different behaviour in writing to a file  RSS feed

 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers!
I'm trying to make a program to read from a file and for each line in the file substitute space charachters (1 or more ("\s+")) with 1 tab ("\t").
Then the line is written to a new file.
The class works fine on my xp laptop with Eclipse at home, while it writes just little squares in the output here at work, with a w2000 pc using Netbeans.
Input file contains ascii characters.
Some ideas?
here you are the class.
Thank you!
Gianni

package regexp;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.*;

public class RepSpace {

/** Creates a new instance of RepSpace */
public RepSpace() {

File fin = new File("c:\\Input.txt");
File fout = new File("c:\\Output.txt");
BufferedReader bin;
BufferedWriter bwr;
String inputLine = new String();
String tabString = new String();
Pattern pattern = Pattern.compile("\\s+");
Matcher matcher;

try{
bin = new BufferedReader(new FileReader(fin));
bwr = new BufferedWriter(new FileWriter(fout));
while ((inputLine = bin.readLine())!= null){
matcher = pattern.matcher(inputLine);
tabString = matcher.replaceAll("\t");
System.out.println(tabString);
bwr.write("\n");
}
bwr.close();
bin.close();
}
catch(IOException ioe){
System.out.println(ioe.getMessage());
}
catch(RuntimeException re){
System.out.println(re.getMessage());
System.out.println(inputLine);
}
}

public static void main(String[] args){
RepSpace rs = new RepSpace();
}

}
 
Lakshmi Ganesh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am a newbie
So hope this solution works for you.
bwr.write("\n");
Here the \n character needs to be \r\n because this is the character that works in most situations as a line break.
One more thing did not understand how
System.out.println(tabString);
will write to the output file.
I was wondering if you also need to add this
bwr.write(tabString);

Let me know if this solution works
 
Lakshmi Ganesh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As a safer bet
instead of using
bwr.write("\n");
or
bwr.write("\n");
try this for better compatibility across OSs
bwr.newLine();
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!