473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Detecting a new line character

I am reading in words from a file using the following code:

Expand|Select|Wrap|Line Numbers
  1. scannedWord = scan.next(); 
  2. System.out.println("word: " + scannedWord);
Then I am writing each word scanned into a file with the following code:

Expand|Select|Wrap|Line Numbers
  1. FileWriter outFile = new FileWriter(outputFile,true);
  2. outFile.write(scannedWord + " ");
  3. outFile.close();
The problem is, that when it writes to the output file, it doesn't remember where spaces are or characer returns.. How do I detect a new line character so that the output file looks exactly like the input file? Thanks!!
Apr 13 '08 #1
2 3265
Ganon11
3,652 Expert 2GB
Assuming that your scan variable is a Scanner, you use use the nextLine() method. This will get the whole line and, when printed out (or written), will preserve spacing.

In order to get individual words out of this, you can use String.split().
Apr 13 '08 #2
sukatoa
539 512MB
I am reading in words from a file using the following code:

Expand|Select|Wrap|Line Numbers
  1. scannedWord = scan.next(); 
  2. System.out.println("word: " + scannedWord);
Then I am writing each word scanned into a file with the following code:

Expand|Select|Wrap|Line Numbers
  1. FileWriter outFile = new FileWriter(outputFile,true);
  2. outFile.write(scannedWord + " ");
  3. outFile.close();
The problem is, that when it writes to the output file, it doesn't remember where spaces are or characer returns.. How do I detect a new line character so that the output file looks exactly like the input file? Thanks!!
Could you post an example input?!! to be saved in a file.....
Apr 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Kevin Frey | last post by:
Hello, Can anyone suggest a way, that does not involve writing a temporary file, that would permit me to discover whether writing: output_file_stream << '\n'; will write either a LF (eg....
2
by: Frank | last post by:
Hello, I'm a total newbie on Javascript, and with a lot of help and some copy and paste managed to get things running. On my site: http://home.wanadoo.nl/homepage I use a form from Bravenet....
11
by: Dag Sunde | last post by:
We have this web-app that requires Javascript to be enabled in the UA (this is okay'ed by our customer). Now I want to make a "pre" page before the login screen, informing the user that he/she...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
7
by: iaesun | last post by:
hello, this post assumes executing a program with file redirection as follows: program.exe < intput.txt where input.txt is a text file where there is not necessarily any whitespace after...
5
by: Jason | last post by:
I have a div box with a border, and some padding left and right. It has some text with different font sizes, and a few icons. I want to cut off any overflow at the end of the line with "...",...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a KeyDown event in which I would like to determine if the current key is a standard character (i.e., from the ASCII character set). In C I would use something like the isascii...
9
by: helraizer1 | last post by:
ChatPad The layout so far is pretty amateur but it'll improve. I spent an 1h30/2h or so in this tonight. The concept of ChatPad is basically a dynamic image acting as a shoutbox. <?php ...
5
by: alex21 | last post by:
I am trying to write a function for determining the data type of columns in a delimited file. However my function is not detecting a newline and exiting the loop after the end of the first line. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.