aspose file tools*
The moose likes Beginning Java and the fly likes Simple File IO Question...Please Help!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple File IO Question...Please Help!!!" Watch "Simple File IO Question...Please Help!!!" New topic
Author

Simple File IO Question...Please Help!!!

Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
So i have to import a file using a fileInputStream and then take out all the extra white spaces and then output the file into a different file obviously using a fileOutputStream. For the life of me I cant figure out how to remove the extra white spaces. Here is my code and all that good stuff so far:

Code:

import java.io.*;
import java.util.*;
public class FileIO {
public static void main (String[] args){
Scanner inputStream = null;

try{
inputStream = new Scanner (new FileInputStream ("Lab6Input.txt"));
}

catch (FileNotFoundException e){
System.out.println("There is no file with that name");
System.exit(0);
}

String firstLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String secondLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String thirdLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String array[];

System.out.println(firstLine);
System.out.println(secondLine);
System.out.println(thirdLine);

}
}

The input file:

Now is the time for all
good people to come to the
aid of their computers.

What the output file should look like:

Now is the time for all
good people to come to the
aid of their computers.


THANKS FOR YOUR HELP!!!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I'm not sure what you mean by extra white space.

Can you give an example?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
try reposting your code but use the code tags on either side of your code snippit (see the "Instant UBB Code" on the bottom of your edit box for the proper tags). Also, put your input and output files in the same tags. I think that you lost all of your white space when you posted the files without the code tags.

Also, if the white space is on either side of your strings, do you know of any functions that can check the characters at these particular locations? If found, do you know of any string functions to get rid of them? Look in the docs under string functions and you'll find lots of useful stuff.
[ March 11, 2007: Message edited by: pete stein ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Please don't post the same question to more than one forum. See here for an explanation of why I just deleted the other copy of this thread.


[Jess in Action][AskingGoodQuestions]
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
ok sorry to Ernest...

and to Pete,

I have no idea how to do that whole ubb thing. I am using Eclipse and i didnt see anything that said ubb in edit.

the extra whitespaces in my input code are missing but they dont matter. they can be anywhere. I just cant figure out how to get rid of more than one between each word. It should read just like in the output. I tried using the "trim()" method but that had no effect. I think i need to read the words by themselves and then call "trim" but i cant figure out how to do this. Im new to this stuff and its kicking my rear I also looked in my book for string commands and trim was the only promising one in there that i noticed.

thanks for your help...i hope this will further help you help me! lol thanks again
Ra Carter
Ranch Hand

Joined: Jul 30, 2005
Posts: 96
well in the beginning of your code you are using Scanner to read in your file. if i am not mistaken Scanner's main purpose is not for reading files. it is for tokenizing String objects. it just so happens that it can take a file stream as input. so look carefully at the api for that object and it should help you accomplish the algorithm you just stated.

if you're interested this probably can be done with regular expressions as well.

about the ubb code thing by the way, what they are talking about is that when you are making comments on this forum, there are some buttons on the right of the stupid smiley faces... use the one labeled CODE to enter in your source code...
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
ok so here is the code that I have and i put a comment block in where I think the remove whitespace code goes



here is the input and output and how it should look

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Can you use regular expressions?
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
yes I can. That was not specified in the assignment. But I figured it out...gosh it took me forever. Thanks to everyone that helped me. Here is the snipit of code i needed to get it to work. I ran this code 3 separate times with slightly different variables for the three separate lines that i was inputing.



thanks again guys...this site is a big help...now should i delete this whole post thing since i no longer need help?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

I ran this code 3 separate times with slightly different variables for the three separate lines that i was inputing.


Isn't it a bit of an overkill to have three copies of the same code? Just put the code into a private method, that takes a work string as a parameter, and that returns the result as a string.

BTW, if you are allowed to use regex, then this would work...

firstLine = firstLine.replaceAll("\\s+", " ");

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ra Carter
Ranch Hand

Joined: Jul 30, 2005
Posts: 96
so what happens if you are given a file with 4 lines?

You don't really need a Scanner object in the approach you took. There is also a String.split method that could have helped you out here. Using scanner to use tokens could have gone along these lines:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple File IO Question...Please Help!!!
 
Similar Threads
What is wrong?
Write a program that reads and writes from binary or text files
HELP w/Scanner question 41 in final test in examlab
Problem in WebServer
reading values from txt file via loop and storing them into class instances