This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

different behaviour in writing to a file  RSS feed

 
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();
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!