java io 操作是基础中的基础,下面列出常见的IO 操作
1,读取本地Txt 文档
File file = new File("e://****.txt");
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String line = "";
StringBuffer sb = new StringBuffer();
while((line = br.readLine())!= null)
{
sb.append(line+"\n");
System.out.println(line);
}
//System.out.println(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
fr.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2,写字符到本地文件
private static final String SEPARATOR = System.getProperty("file.separator");
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
File file = new File("f:"+SEPARATOR+"File_Test"+SEPARATOR+"test.txt");
if(!file.exists())
{
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
fw.append("charlie");
fw.append("\n");
fw.append("only");
fw.append("have fun");
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3,读写文件的五种写法
/**
* 读写一个文件测试
*
* 输入输出 都用 Buffer
*/
public static void copyFileByBuffer()
{
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream("e://Git权威指南.pdf");
bis = new BufferedInputStream(fis);
fos = new FileOutputStream("D://git.pdf");
bos = new BufferedOutputStream(fos);
int length = 0;
byte[] bytes = new byte[1024];
long start = System.currentTimeMillis();
while((length = bis.read(bytes))!= -1)
{
bos.write(bytes, 0, length);
}
System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
fis.close();
bis.close();
bos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 输出 buffer
*/
public static void copyFileByBufferOutput()
{
FileInputStream fis = null;
//BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream("e://Git权威指南.pdf");
// bis = new BufferedInputStream(fis);
fos = new FileOutputStream("D://git.pdf");
bos = new BufferedOutputStream(fos);
int length = 0;
byte[] bytes = new byte[1024];
long start = System.currentTimeMillis();
while((length = fis.read(bytes))!= -1)
{
fos.write(bytes, 0, length);
}
System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
fis.close();
bos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 输出 buffer
*/
public static void copyFileByBufferedInputStream()
{
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("e://Git权威指南.pdf");
bis = new BufferedInputStream(fis);
fos = new FileOutputStream("D://git.pdf");
int length = 0;
byte[] bytes = new byte[1024];
long start = System.currentTimeMillis();
while((length = bis.read(bytes))!= -1)
{
fos.write(bytes, 0, length);
}
System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* NoBuffer
*/
public static void copyFileByNoBuffer()
{
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("e://Git权威指南.pdf");
fos = new FileOutputStream("D://git.pdf");
int length = 0;
byte[] bytes = new byte[1024];
long start = System.currentTimeMillis();
while((length = fis.read(bytes))!= -1)
{
fos.write(bytes, 0, length);
}
System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* NoBuffer
*/
public static void copyFileByReader()
{
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("e://签名信息.txt");
fw = new FileWriter("D://git.txt");
int length = 0;
char str = ' ';
char[] chars = new char[1024];
byte[] bytes = new byte[1024];
long start = System.currentTimeMillis();
while((length = fr.read(chars))!= -1)
{
fw.write(chars, 0, length);
}
System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
fr.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}