-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fileread.java
83 lines (61 loc) · 1.6 KB
/
Fileread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.*;
import java.lang.*;
import java.util.*;
/**
* Open and read a file, and return the lines in the file as a list
* of Strings.
* (Demonstrates Java FileReader, BufferedReader, and Java5.)
*/
class Fileread{
//File f = new File("querie.txt");
public List<String> readFile(String filename)
{
List<String> records = new ArrayList<String>();
try
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null)
{
records.add(line);
}
reader.close();
return records;
}
catch (Exception e)
{
System.err.format("Exception occurred trying to read '%s'.", filename);
e.printStackTrace();
return null;
}
}
}
// import java.io.*;
// class FileRead
// {
// public static void main(String args[])
// {
// try{
// // Open the file that is the first
// // command line parameter
// File f = new File("querie.txt");
// FileInputStream fstream = new FileInputStream(f);
// // 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 = br.readLine()) != null) {
// // Print the content on the console
// String[] arr = str.split(" ");
// for(int i=0 ; i<str.length() ; i++){
// arr[i] = in.readLine();
// }
// }
// //Close the input stream
// in.close();
// }catch (Exception e){//Catch exception if any
// System.err.println("Error: " + e.getMessage());
// }
// }
// }