0% found this document useful (0 votes)
8 views1 page

Reading Text Files in Java

The document describes two methods for reading text from a file in Java. The first method uses a FileReader and BufferedReader to read each line of a text file and set it to the text of a JTextBox. The second method uses a FileInputStream, DataInputStream, and BufferedReader to read each line of a text file and print it to the console. Both catch potential IOExceptions.

Uploaded by

Mohamed Roua
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Reading Text Files in Java

The document describes two methods for reading text from a file in Java. The first method uses a FileReader and BufferedReader to read each line of a text file and set it to the text of a JTextBox. The second method uses a FileInputStream, DataInputStream, and BufferedReader to read each line of a text file and print it to the console. Both catch potential IOExceptions.

Uploaded by

Mohamed Roua
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Read all Texte in TextBox



import [Link].*;
class Test1 {
public static void main (String [] arg){
JTextBox botext = new JTextBox() ;
File f = new File("[Link]");
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String c ;
while((c =[Link]())!=null){
[Link] (c);
}
[Link]();
}
catch(IOException i){
[Link]("Error");
}
}
}
2. 2
eme
Methode
import [Link].*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("[Link]");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = [Link]()) != null) {
// Print the content on the console
[Link] (strLine);
}
//Close the input stream
[Link]();
}catch (Exception e){//Catch exception if any
[Link]("Error: " + [Link]());
}
}
}
Mohlaid0892@[Link]

You might also like