Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Advice:</p> <ol> <li><p>Indent your code properly. The stuff in your question looks like a dog's breakfast.</p></li> <li><p>You don't need to initialize <code>f</code> inside the try / catch block. The constructor can't throw an <code>Exception</code> the way you are using it.</p></li> <li><p>In fact, you don't need to declare it at all. Just inline the <code>new File(...)</code>.</p></li> <li><p>In fact, you don't even need to do that. Use the <code>FileReader(String)</code> constructor.</p></li> <li><p>There's no point initializing the <code>StringBuffer</code> inside the loop. The potential performance benefit is small and only applies in the edge-case where the file is empty or doesn't exist. In all other cases, this is an anti-optimization.</p></li> <li><p>Don't catch <code>Exception</code>. Catch the exceptions that you expect to be thrown and allow all other exceptions to propagate. The unexpected exceptions are going to be due to bugs in your program, and need to be handled differently from others.</p></li> <li><p>When you catch an exception, don't throw away the evidence. For an unexpected exception, either print / log the exception, its message and its stacktrace, or pass it as the 'cause' of the exception that you throw.</p></li> <li><p>The <code>FileReader</code> should be closed in a <code>finally</code> clause. In your version of the code, the <code>FileReader</code> won't be closed if there is an exception after the object has been created and before the <code>close()</code> call. That will result in a leaked file descriptor and <em>could</em> cause problems later in your application.</p></li> <li><p>Better yet, use the new Java 7 "try with resource" syntax which takes care of closing the "resource" automatically (see below).</p></li> <li><p>You are reading from the file one character at a time. This is <strong>very</strong> inefficient. You need to either wrap the <code>Reader</code> in a <code>BufferedReader</code>, or read a large number of characters at a time using (for example) <code>read(char[], int, int)</code></p></li> <li><p>Use <code>StringBuilder</code> rather than <code>StringBuffer</code> ... unless you <em>need</em> a thread-safe string assembler.</p></li> <li><p>Wrapping exceptions in <code>RuntimeException</code> is bad practice. It makes it difficult for the caller to handle specific exceptions ... if it needs to ... and even makes printing of a decent diagnostic more difficult. (And that assumes that you didn't throw away the original exception like your code does.)</p></li> </ol> <p>Note: if you follow the advice of point 8 and not 9, you will find that the initialization of <code>fr</code> to <code>null</code> is necessary if you open the file in the <code>try</code> block.</p> <hr> <p>Here's how I'd write this:</p> <pre><code>public String readFile() throws IOException { // Using the Java 7 "try with resource syntax". try (FileReader fr = new FileReader("c:/test.txt")) { BufferedReader br = new BufferedReader(fr); StringBuilder content = new StringBuilder(); int c; while ((c = br.read()) != -1) { content.append((char)c); } return content.toString(); } } </code></pre> <p>A further optimization would be to use <code>File.length()</code> to find out what the file size (in bytes) is and use that as the initial size of the <code>StringBuilder</code>. However, if the files are typically small this is likely to make the application slower.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload