-3

I have the following task that have to be implemented in Java.

I have a String that represent the path of a txt file, something like this:

String fileFatturePa = "C:\\Users\\Andrea\\Desktop\\D150316.T1642\\myFile.txt";

This textual file contains some text (that represent XML but this is not important because I do not have to operate on it with something like XPath but I have to do simple substring operation).

So I need to read this textual file (starting from its path) and then I have to do some textual operation.

This file could be very big.

What is the best way to read it stargint from its path?

NickL
  • 4,132
  • 2
  • 19
  • 38
AndreaNobili
  • 34,200
  • 85
  • 240
  • 456
  • 4
    What have you done? Show some code, solve some problems. The Java API javadocs are there for your readng pleasure, and shoving "open file for reading" into a browser web search is easy. –  Jun 09 '15 at 13:58
  • what do you mean by working on a file? do you want to read from it or write to it – GregH Jun 09 '15 at 14:00
  • possible duplicate of [Best way to read a text file](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file) – Crembo Jun 09 '15 at 14:00
  • 5
    Out of curiosity: how do you reach a reputation of 4000 ... and then come here and ask questions like a complete newbie? – GhostCat Jun 09 '15 at 14:00
  • I will add that because of the nature of XML files, if you go down the path of treating this like a plain text file, and you want to make changes to it, you are going to have a bad day. Use one of the XML libraries out there to walk the elements and make your changes. –  Jun 09 '15 at 14:00

3 Answers3

2

A good starting point is the official Java documentation by Oracle: Basic I/O.

Atafar
  • 675
  • 5
  • 8
1
 BufferedReader reader = new BufferedReader(new FileReader(fileFatturePa));

        try
        {                           
            String line = null;         
            while ((line = reader.readLine()) != null)
            {
                  ***operations(specific to each line of the text file) ***
            }               
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }               

        finally
        {
            reader.close();
        }           
guy_sensei
  • 485
  • 5
  • 19
0

Simple, you create a new File: File f = new File(path). For raed all the lines, you can use the class Fileutils from Apache Commons IO. And you will have something like: List<String> allLines=Fileutils.readlines(f)

Nicola Bena
  • 78
  • 1
  • 1
  • 9
  • "This file could be very big." Now, how to solve that pesky `OutOfMemoryError` without redesigning the whole file reader... –  Jun 09 '15 at 14:06
  • I found this article yesterday: http://www.baeldung.com/java-read-lines-large-file – Nicola Bena Jun 09 '15 at 14:10
  • Yes, and your example is **not** one of the "streaming" or "iterator" based approaches discussed therein. The example you give can make the heap grow unbounded as the file size grows. –  Jun 09 '15 at 14:13