Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

different behaviour in writing to a file

 
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();
}

}
 
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();
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!