设为首页 加入收藏

TOP

Java读写文件方法总结(二)
2015-11-10 13:45:07 来源: 作者: 【 】 浏览:11
Tags:Java 读写 文件 方法 总结
? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? if (reader != null) {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? reader.close();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? * 随机读取文件内容
? ? * */
? ? public static void readFileByRandomAccess(String filename) {
? ? ? ? RandomAccessFile randomfile=null;
? ? ? ? try {
? ? ? ? ? ? System.out.println("随机读取一段文件内容");
? ? ? ? ? ? randomfile=new RandomAccessFile(filename,"r");
? ? ? ? ? ? long fileLength=randomfile.length();
? ? ? ? ? ? int beginIndex=(fileLength > 4 ? 4 : 0);
? ? ? ? ? ? randomfile.seek(beginIndex);
? ? ? ? ? ? byte[] bytes=new byte[10];
? ? ? ? ? ? int byteread=0;
? ? ? ? ? ? while ((byteread=randomfile.read(bytes)) != -1) {
? ? ? ? ? ? ? ? System.out.write(bytes,0,byteread);
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? if (randomfile != null) {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? randomfile.close();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? private static void showAvailableBytes(InputStream in) {
? ? ? ? try {
? ? ? ? ? ? System.out.println("当前字节输入流中的字节数为:" + in.available());
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public static void main(String[] args) {
? ? ? ? String filename="E:\\BaiYiShaoNian.txt";
? ? ? ? JavaIO.readFileByBytes(filename);
? ? ? ? JavaIO.readFileByChar(filename);
? ? ? ? JavaIO.readFileByLine(filename);
? ? ? ? JavaIO.readFileByRandomAccess(filename);
? ? }
}


Java写文件


package Linux公社;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;


public class JavaIO2 {


? ? public static void main(String[] args) throws IOException {
? ? ? ? String Path="E:\\Linux公社\\JAVA";
? ? ? ? File file=new File("E:\\Linux公社\\JAVA","BaiYiShaoNian.txt");
? ? ? ? if (!file.exists()) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? file.createNewFile();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /**
? ? ? ? * Java写入文件的三种方法
? ? ? ? * */
? ? ? ? FileOutputStream fos=null;
? ? ? ? BufferedWriter bw=null;
? ? ? ? FileWriter fw=null;
? ? ? ? int value=1000;
? ? ? ?
? ? ? ? try {
? ? ? ? ? ? fos=new FileOutputStream(new File(Path+"fos.txt"));
? ? ? ? ? ? long begin=System.currentTimeMillis();
? ? ? ? ? ? for (int i=1; i<=value; i++) {
? ? ? ? ? ? ? ? fos.write(5);
? ? ? ? ? ? }
? ? ? ? ? ? long end=System.currentTimeMillis();
? ? ? ? ? ? System.out.println("TheCostTime of FileOutputStream is : " + (end-begin));
? ? ? ? ? ? fos.close();
? ? ? ? ? ?
? ? ? ? ? ? bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8"));
? ? ? ? ? ? begin=System.currentTimeMillis();
? ? ? ? ? ? for (int i=1; i<=value; i++) {
? ? ? ? ? ? ? ? bw.write(5);
? ? ? ? ? ? ? ? bw.newLine();
? ? ? ? ? ? }
? ? ? ? ? ? bw.close();
? ? ? ? ? ? end=System.currentTimeMillis();
? ? ? ? ? ? System.out.println("TheCostTime of BufferedWriter is : " + (end-begin));
? ? ? ? ? ?
? ? ? ? ? ? fw=new FileWriter(Path+"fw.txt");
? ? ? ? ? ? begin=System.currentTimeMillis();
? ? ? ? ? ? for (int i=1; i<=value; i++) {
? ? ? ? ? ? ? ? fw.write(5);? ? ? ? ? ? ? ?
? ? ? ? ? ? }
? ? ? ? ? ? fw.close();
? ? ? ? ? ? end=System.currentTimeMillis();
? ? ? ? ? ? System.out.println("TheCostTime of FileWriter is : " + (end-begin));
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? // TODO Auto-generated catch block
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? fos.close(); //FileOutputStream
? ? ? ? ? ? ? ? bw.close(); //BufferedWriter
? ? ? ? ? ? ? ? fw.close(); //FileWriter
? ?

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java NIO入门 下一篇Java学习:飘逸的字符串

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: