0

Possible Duplicate:
How to create a Java String from the contents of a file
How to set a java string variable equal to “htp://website htp://website ”

I am making aprogram because I have about 50 pages of a word document that is filled with both emails and other text. I want to filter out just the emails. I wrote the program to do this which was very simple, not I just need to figure away to store the pages in a string variable.

I have a text file imported into my code File f=new File("test.txt"); Now I just need to find a way to save the text in this file into a String variable. Any help?

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
  • Do you have any code so far? Please show us the code that extracts the data. Do you want to store the emails in a text file? – tckmn Dec 13 '12 at 00:44
  • Be more specific! What do you mean by "store the pages" ? The whole text or just the filtered words? Also, show some code. – Lotzki Dec 13 '12 at 00:45
  • Well, in LibreOffice, you can search all the occurrences of a regex, in one shot. But probably it's not what you're looking for. – ignis Dec 13 '12 at 00:46
  • First, post the library you're using to work with word documents. Second, post what have you tried. Third, do not edit a question to add more questions, instead post a new question, or both questions could be closed. – Luiggi Mendoza Dec 13 '12 at 00:53
  • Ok so I have a text file imported into my code File f=new File("test.txt"); Now I just need to find a way to save the text in this file into a String variable. – vegetablelasagna Dec 13 '12 at 01:35
  • Start by learning about the Java I/O libraries. There are plenty of tutorials online. – Code-Apprentice Dec 13 '12 at 01:40
  • related: http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string – Paul Bellora Dec 13 '12 at 01:41
  • Have you ever seen my answer to your other question? If not, see it here: http://stackoverflow.com/a/13852139/540552 – Victor Stafusa Dec 13 '12 at 19:37

1 Answers1

0

Okay, so based on your edit you're working with a text file and not a word file (two very different things ;) )

This is pretty simple, you just use a scanner class as such:

    File file = new File("data.txt");

    String s = new String();
    try{
        Scanner sc = new Scanner(file);

        while (sc.hasNextLine()) {
            s += sc.nextLine();
        }

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

Now bear in mind, if you just want a single line at a time from your text file (I have no idea what it is you're looking for, or what the data looks like) it's more likely that you'd want to store your data as an array of Strings or a linked list

darkpbj
  • 2,473
  • 3
  • 19
  • 29