What's the difference between File and FileLoader in Java? -
so have following code should read text file (this main class):
import gui.menuwindow; import java.io.ioexception; import javax.swing.joptionpane; public class assessor { public static void main(string args[]) throws ioexception { fileloader file = new fileloader("example.txt"); try{ new menuwindow(file.loader()); } catch(exception exc) { joptionpane.showmessagedialog(null, "error reading file"); } } }
then i'd have load text listbox using swing. thing i've found new code read text file:
import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.filenotfoundexception; import java.io.ioexception; public class readtextfileexample { public static void main(string[] args) { file file = new file("test.txt"); stringbuffer contents = new stringbuffer(); bufferedreader reader = null; try { reader = new bufferedreader(new filereader(file)); string text = null; // repeat until lines read while ((text = reader.readline()) != null) { contents.append(text) .append(system.getproperty( "line.separator")); } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { try { if (reader != null) { reader.close(); } } catch (ioexception e) { e.printstacktrace(); } } // show file contents here system.out.println(contents.tostring()); } }
so i'd know difference between following 2 lines:
fileloader file = new fileloader("example.txt"); //first code file file = new file("test.txt"); //second code
and... what's stringbuffer
, bufferedreader
used to? thanks!
so i'd know difference between following 2 lines:
fileloader file = new fileloader("example.txt"); //first code file file = new file("test.txt"); //second code
the first creates java.io.fileloader
andreas discusses. since javadoc says "the constructors of class assume default character encoding , default byte-buffer size appropriate", should never used.
the second creates java.io.file
file path utility methods can used read directory trees, delete, create, , move files, etc., or can used fileinputstream
, other classes access file contents.
and... what's stringbuffer , bufferedreader used to? thanks!
the stringbuffer
used collect contents of file.
the bufferedreader
used speed reading of file. instead of reading 1 character @ time, bufferedreader
batches reads using internal buffer.
Comments
Post a Comment