A Filereader and Writer, with a little work


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: A Filereader and Writer, with a little work

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Posts
    1

    Question A Filereader and Writer, with a little work

    Greetings all,
    The aim of this class is to take a text file, and identify each unique word. I want to save the words into an array, and use each words' place in the array as an I.D. If the word is new, place it at the end of the array and print its id. If the word has been encountered before, I just want to print its I.D. The printed I.D's will make up a new text file, in QDimacs format.
    So far I have a BufferedReader and BufferedWriter, Ive tried reading in a text document, and I'm getting a new text document in the required format, but its blank. Any idea's?

    Here's the code,

    package Converter;

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.File;

    import java.util.List;
    import java.util.ArrayList;

    public class WordReader {

    public WordReader(File file) {
    convert(file);
    }

    // take String args from FileSelector
    private void convert(File file) {

    // will store the words read from the file
    List<String> wordList = new ArrayList<String>();
    String fileName = file.toString() + ".qdimacs";

    try {
    // attempt to open the words file, and the new qdimacs file
    BufferedReader br = new BufferedReader(new FileReader(file));
    BufferedWriter wr = new BufferedWriter(new FileWriter(fileName));

    String word = br.readLine();
    String[] words = null;
    int i;
    int x;

    // loop and read a line from the file as long as we dont
    // get null

    while (word != null) {
    words = word.split(" ");
    // compare the words to those in the arrayList

    for (i = 1; i < wordList.size(); i++) {
    String s = wordList.get(i);

    for (x = 0; x < words.length; x++) {
    String w = words[x].toString();

    if ((s.equals(w)) == true) {
    // if the word has been seen before, print the words place in the array
    wr.write(i);
    wr.newLine();
    }
    // If the word hasn't been seen before, place it on the end of the
    // array and print its place.
    else
    wordList.add(word);
    wr.write(wordList.size());
    }
    }
    br.close();
    wr.close();
    }

    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Does your code work as expected if you use the "append" signal in creating your FileWriter object? [sounds like you are experiencing the only content of your file is a new line, which is why your file is blank]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center