Reputation: 23
What I am trying to do here is read a text file from my system and print it's contents in java I have also tried giving permissions to folder in which it is present please have a look at my code:
public class Rtree {
public static void main(String... args)
{
// ArrayList<String> st=new ArrayList<String>();
try{
FileReader file=new FileReader("D://Qos Logs");
//DataInputStream In=new DataInputStream(fstream);
BufferedReader br=new BufferedReader(file);
String s;
while((s =br.readLine()) !=null){
// text=
System.out.println(s);
}
br.close();
// In.close();
} catch(Exception e){
System.err.println("Error:"+e.getMessage());
}
}
}
Upvotes: 1
Views: 1692
Reputation: 331
You can read data using File and Scanner class simply pass the address of the file in File class Constructor and used the object of a class in your Scanner constructor next code in front of you.
try {
File file = new File("Path of your file include file name with extension like C:\\Users\\kashi\\Desktop\\DNA.txt");
if(file.exists()){
System.out.println("yes");
Scanner readFile = new Scanner(file);
if(file.canRead()){
System.out.println("yes you can read it");
if(readFile.hasNext()){
System.out.println(readFile.nextLine());
}
}
}else {
System.out.println("No");
}
}catch (Exception e){
System.out.println(e.getMessage());
}
Upvotes: 0
Reputation: 445
There are many ways of reading the file, but I'll show the simplest one. Use \\
slash when you give the full path of your file. Separating the folders and your file name by \\
.
Try doing this in your code :
FileReader file=new FileReader("D:\\path of your file\\ 20111123.txt");
Upvotes: 1