java 文件读写_FileReader
2009-12-30 21:14:37 来源:WEB开发网核心提示:package test;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class Test { public static void main(Stri
package test;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String arg[]) {
String fileName = "E:\\share\\test.txt";
String writeData = "HelloWorld!\r\nnihao我的内存是3G的 ";
File file = new File(fileName);
if(file.exists()){
file.delete();
}
char[] byteOutData = writeData.toCharArray();
char[] byteInData = new char[50];
int length = 0;
try {
file.createNewFile();
if(file.exists() && file.canWrite()){
FileWriter fileWrite = new FileWriter(file);
fileWrite.write(byteOutData);
fileWrite.close();
}
if (file.exists() && file.canRead()) {
FileReader fileReader = new FileReader(file);
while((length = fileReader.read(byteInData)) !=-1){
System.out.PRint(new String(byteInData,0,length));
}
fileReader.close();
}
} catch (IOException e) {
System.out.println("IOException occur");
e.getMessage();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/whb3118/archive/2009/12/29/5099733.aspx
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String arg[]) {
String fileName = "E:\\share\\test.txt";
String writeData = "HelloWorld!\r\nnihao我的内存是3G的 ";
File file = new File(fileName);
if(file.exists()){
file.delete();
}
char[] byteOutData = writeData.toCharArray();
char[] byteInData = new char[50];
int length = 0;
try {
file.createNewFile();
if(file.exists() && file.canWrite()){
FileWriter fileWrite = new FileWriter(file);
fileWrite.write(byteOutData);
fileWrite.close();
}
if (file.exists() && file.canRead()) {
FileReader fileReader = new FileReader(file);
while((length = fileReader.read(byteInData)) !=-1){
System.out.PRint(new String(byteInData,0,length));
}
fileReader.close();
}
} catch (IOException e) {
System.out.println("IOException occur");
e.getMessage();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/whb3118/archive/2009/12/29/5099733.aspx
更多精彩
赞助商链接