java基本的文件读写
1
package io;
2
3
import java.io.*;
4
5
/**
6
* @author
7
* @version 1.0
8
*/
9
public class ReadLineFromFile
10
{
11
/**
12
* Attributes
13
*/
14
private BufferedReader br;
15
private String filePath;
16
17
/**
18
* Constructor
19
*/
20
public ReadLineFromFile()
21
{
22
}
23
24
/**
25
* Constructor
26
*/
27
public ReadLineFromFile(String filePath)
28
{
29
this.filePath = filePath;
30
}
31
32
/**
33
* Method that will set the file path
34
*
35
* @param filePath
36
*/
37
public void setFilePath(String filePath)
38
{
39
this.filePath = filePath;
40
}
41
42
/**
43
* Method that will return the file path
44
*
45
* @return string, string that indicates file path
46
*/
47
public String getFilePath()
48
{
49
return this.filePath;
50
}
51
52
/**
53
* Method that will open the file
54
*/
55
public void open() throws Exception
56
{
57
FileReader fr = new FileReader(this.filePath);
58
br = new BufferedReader(fr);
59
}
60
61
/**
62
* Method that will open the file with the file path specified
63
*
64
* @param filePath
65
*/
66
public void open(String filePath) throws Exception
67
{
68
FileReader fr = new FileReader(filePath);
69
br = new BufferedReader(fr);
70
}
71
72
/**
73
* Method that will read one line and return the string
74
*
75
* @return String, line read
76
*/
77
public String readLine() throws Exception
78
{
79
return br.readLine();
80
}
81
82
/**
83
* Method that will read one byte from the file
84
*
85
* @return int, byte read
86
*/
87
public int read() throws Exception
88
{
89
return br.read();
90
}
91
92
/**
93
* Method that will close the file
94
*/
95
public void close() throws Exception
96
{
97
br.close();
98
}
99
}
100
package io;2

3
import java.io.*;4

5
/**6
* @author 7
* @version 1.08
*/9
public class ReadLineFromFile10
{11
/**12
* Attributes13
*/14
private BufferedReader br;15
private String filePath;16
17
/**18
* Constructor19
*/20
public ReadLineFromFile()21
{22
}23
24
/**25
* Constructor26
*/27
public ReadLineFromFile(String filePath)28
{29
this.filePath = filePath;30
}31
32
/**33
* Method that will set the file path34
* 35
* @param filePath36
*/37
public void setFilePath(String filePath)38
{39
this.filePath = filePath;40
}41
42
/**43
* Method that will return the file path44
* 45
* @return string, string that indicates file path46
*/47
public String getFilePath()48
{49
return this.filePath;50
}51
52
/**53
* Method that will open the file54
*/55
public void open() throws Exception56
{57
FileReader fr = new FileReader(this.filePath);58
br = new BufferedReader(fr);59
}60
61
/**62
* Method that will open the file with the file path specified63
* 64
* @param filePath65
*/66
public void open(String filePath) throws Exception67
{68
FileReader fr = new FileReader(filePath);69
br = new BufferedReader(fr);70
}71
72
/**73
* Method that will read one line and return the string74
* 75
* @return String, line read76
*/77
public String readLine() throws Exception78
{79
return br.readLine();80
}81
82
/**83
* Method that will read one byte from the file84
* 85
* @return int, byte read86
*/87
public int read() throws Exception88
{89
return br.read();90
}91
92
/**93
* Method that will close the file94
*/95
public void close() throws Exception96
{97
br.close();98
}99
}100

1
package io;
2
3
import java.io.*;
4
5
/**
6
*
7
* @version 1.0
8
*/
9
public class WriteLineToFile
10
{
11
/**
12
* Attributes
13
*/
14
private PrintWriter pw;
15
private String filePath;
16
17
/**
18
* Constructor
19
*/
20
public WriteLineToFile()
21
{
22
}
23
24
/**
25
* Constructor
26
*/
27
public WriteLineToFile(String filePath)
28
{
29
this.filePath = filePath;
30
}
31
32
/**
33
* Method that will set the file path
34
*
35
* @param filePath
36
*/
37
public void setFilePath(String filePath)
38
{
39
this.filePath = filePath;
40
}
41
42
/**
43
* Method that will return the file path
44
*
45
* @return string
46
*/
47
public String getFilePath()
48
{
49
return this.filePath;
50
}
51
52
/**
53
* Method that will open the file
54
*/
55
public void open() throws Exception
56
{
57
FileWriter fw = new FileWriter(this.filePath);
58
pw = new PrintWriter(fw);
59
}
60
61
/**
62
* Method that will open the file and specified if the information should be
63
* append to the end of the file or not
64
*
65
* @param append
66
*/
67
public void open(boolean append) throws Exception
68
{
69
FileWriter fw = new FileWriter(this.filePath, append);
70
pw = new PrintWriter(fw);
71
}
72
73
/**
74
* Method that will open the file
75
*
76
* @param filePath
77
*/
78
public void open(String filePath) throws Exception
79
{
80
FileWriter fw = new FileWriter(filePath);
81
pw = new PrintWriter(fw);
82
}
83
84
/**
85
* Method that will open the file with the file path specified and if it
86
* should be append to the end of the file or not
87
*
88
* @param filePath
89
* @param append
90
*/
91
public void open(String filePath, boolean append) throws Exception
92
{
93
FileWriter fw = new FileWriter(filePath, append);
94
pw = new PrintWriter(fw);
95
}
96
97
/**
98
* Method that will write a line to the file
99
*
100
* @param input
101
*/
102
public void writeLine(String input)
103
{
104
pw.println(input);
105
}
106
107
/**
108
* Method that will write a string to the file
109
*
110
* @param input
111
*/
112
public void write(String input)
113
{
114
pw.print(input);
115
}
116
117
/**
118
* Method that will close the file
119
*/
120
public void close()
121
{
122
pw.close();
123
}
124
}
125
package io;2

3
import java.io.*;4

5
/**6
* 7
* @version 1.08
*/9
public class WriteLineToFile10
{11
/**12
* Attributes13
*/14
private PrintWriter pw;15
private String filePath;16
17
/**18
* Constructor19
*/20
public WriteLineToFile()21
{22
}23
24
/**25
* Constructor26
*/27
public WriteLineToFile(String filePath)28
{29
this.filePath = filePath;30
}31
32
/**33
* Method that will set the file path34
* 35
* @param filePath36
*/37
public void setFilePath(String filePath)38
{39
this.filePath = filePath;40
}41
42
/**43
* Method that will return the file path44
* 45
* @return string46
*/47
public String getFilePath()48
{49
return this.filePath;50
}51
52
/**53
* Method that will open the file54
*/55
public void open() throws Exception56
{57
FileWriter fw = new FileWriter(this.filePath);58
pw = new PrintWriter(fw);59
}60
61
/**62
* Method that will open the file and specified if the information should be63
* append to the end of the file or not64
* 65
* @param append66
*/67
public void open(boolean append) throws Exception68
{69
FileWriter fw = new FileWriter(this.filePath, append);70
pw = new PrintWriter(fw);71
}72
73
/**74
* Method that will open the file75
* 76
* @param filePath77
*/78
public void open(String filePath) throws Exception79
{80
FileWriter fw = new FileWriter(filePath);81
pw = new PrintWriter(fw);82
}83
84
/**85
* Method that will open the file with the file path specified and if it86
* should be append to the end of the file or not87
* 88
* @param filePath89
* @param append90
*/91
public void open(String filePath, boolean append) throws Exception92
{93
FileWriter fw = new FileWriter(filePath, append);94
pw = new PrintWriter(fw);95
}96
97
/**98
* Method that will write a line to the file99
* 100
* @param input101
*/102
public void writeLine(String input)103
{104
pw.println(input);105
}106
107
/**108
* Method that will write a string to the file109
* 110
* @param input111
*/112
public void write(String input)113
{114
pw.print(input);115
}116
117
/**118
* Method that will close the file119
*/120
public void close()121
{122
pw.close();123
}124
}125




浙公网安备 33010602011771号