-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathioclass.txt
43 lines (38 loc) · 1.15 KB
/
ioclass.txt
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
package mostafa;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ioclass {
public void stringToFile( String text, String fileName ) {
try {
File file = new File( fileName );
if ( ! file.exists( ) ) {
file.createNewFile( );
}
FileWriter fw = new FileWriter( file.getAbsoluteFile( ) );
BufferedWriter bw = new BufferedWriter( fw );
bw.write( text );
bw.close( );
}
catch( IOException e ) {
System.out.println("Error: " + e);
e.printStackTrace( );
}
} //End method stringToFile
public char[] ReadFileToCharArray(String filePath) throws IOException {
StringBuilder fileData = new StringBuilder(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[10];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString().toCharArray();
}
}