This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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();
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!