views:

65

answers:

2

I'm trying to have my application read through a text file and look for a string. If the string does not exist, it makes it using println. The only problem I'm having is that it doesn't seem to read the text file. What I have so far is:

PrintWriter itemwriter = new PrintWriter(new FileOutputStream(items));
FileInputStream fstream = new FileInputStream(items);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

String strLine;

while ((strLine = br.readLine()) != null)   {
    if (strLine.contains(name)) {
        //do nothing, the item already is in the database.
    } else {
        itemwriter.println(name);
}

That doesn't seem to work though. Any suggestions?

+3  A: 

You're trying to read and write to the same file at the same time. While there may be a way of getting that to work, it's going to be fiddly.

I suggest you read from file A and write to file B - and if you want to effectively replace the input file, then you can do that with a sequence of moves and deletes at the end.

A few other suggestions:

  • You should have try/finally blocks to close both the input and the output at the end.
  • You don't need to use a DataInputStream - you're not using anything from it. Just wrap the input stream directly.
  • I suggest you specify the encoding explicitly both for input and output rather than trusting the default encoding. (I wish FileWriter and FileReader accepted encodings in their constructors.)
  • It's more robust to use OutputStreamWriter (or something similar) rather than PrintWriter - currently you're not going to detect if anything goes wrong when you're writing.

So something like this:

// Ideally have this as a constant somewhere.
Charset utf8 = Charset.forName("UTF-8");

BufferedReader reader = new BufferedReader(new InputStreamReader(
    new FileInputStream(inputFile), utf8);
try {
  try {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
        new FileOutputStream(outputFile), utf8);

    String line;
    while ((line = reader.readLine()) != null) {
      if (!line.contains(name)) {
        writer.write(line);
        writer.newLine();
      }
    }
  } finally {
    writer.close();
  }
} finally {
  reader.close();
}
// Now if you want to shift files around, do it here

It's a shame that Java makes this so fiddly with the try/finally blocks. Oh for C#'s using statements...

Jon Skeet
A: 

First of all you shounld not be using Streams. Use FileReaders and FileWriters.

If you need more help post your SSCCE showing the problem.

camickr
He's already using readers and writers - they're just wrapped around file streams, which is entirely reasonable. IMO the encodings should be specified explicitly, but that's a slightly different matter.
Jon Skeet
`FileReader` and `FileWriter` pick up whatever charset happens to be the default. Much better to be explict, which points to using streams and the converters (`InputStreamReader` and `OutputStreamWriter`.
Tom Hawtin - tackline
And he already post the running code. ( well you have to append 4 lines two of which are `}` )
OscarRyz
Looks like I really don't understand encodings. I would agree that if your program is the only one that creates and reads the file then you are in full control of the encoding.But how do you know how the file is created? Maybe it was created in Word, or Notepad or a another editor. Do these programs not use the default encoding of the OS? In these cases would you not use the default encoding. Again I must admit I've only ever worked on a single OS and have never had problems reading any text file whether is has been emailed, downloaded or created by the any of the above methods.
camickr
The text files that I'm using were created in Notepad, if that helps you at all. I would think they use the system's default encoding.
PuppyKevin
@PuppyKeevin: "text files that I'm using were created in Notepad" ... not necessarily - you should be using a true text editor. Too many user influenced needs make using notepad as a not-text removal tool somewhat dicey. I use it all the time for that purpose but such things as what was used to generate the text file can lead to several issues. I currently use DataInputStream as it should do some of what you are trying to accomplish. What exactly is not working?
Nicholas Jordan