您的位置:首页 > 其它

输入、输出文件函数

2010-09-19 17:20 239 查看
1.BufferedReader 和InputStreamReader

转换流用于实现将字节流转换成字符流,其中InputStreamReader将字节输入字符流转换成字符输入流。OutputStreamWriter将字节输出流转成字符输出流。

由于BufferedReader具有一个readerLine方法,可以非常方便地一次读入一行内容,所以经常把读

取的文本内容输入流包装成BufferedReader,用以方便地读取输入流的文本内容。

InputStreamReader 是字节流通向字符流的桥梁:它使用指定的
charset
读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。

每次调用 InputStreamReader 中的一个 read() 方法都会导致从底层输入流读取一个或多个字节。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。

为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:

BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));


public class PrintStream {
public static void main(String[] args) {
BufferedReader br = null;
//将System.in对象转换成Reader对象,此处用到了转换流
InputStreamReader reader = new InputStreamReader(System.in);
//将普通的Reader包装成BufferedReader
br = new BufferedReader(reader);
String bufString = null;

try {
//采取循环一行一行读取
while((bufString=br.readLine())!=null){
System.out.println("输出内容为:"+bufString);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

注:如果我们需要输出文本内容,都应该将输出流包装成PrintStream后进行输出。

2.RandomAccessFile的只读输入

RandomAccessFile是java输入/输出流体系中工能最丰富的文件内容访问类,它提供了众多的方

法来访问文件内容,它既可以读取文件内容,也可以向文件输出数据。与普通的输入、输出流不同的

是,它支持“随机访问”的方式,程序可以直接跳转到文件的任意地方来读写数据。它的两个方法为
long getFilePointer(): 返回文件记录的当前位置
void seek(long pos):将文件记录指针定位到pos位置

public class RandomAccessFileTest {
public static void main(String[] args) {
RandomAccessFile raf = null;

//raf = new RandomAccessFileTest();
try {
raf = new RandomAccessFile("C://Users//zhang_zhihui//Workspaces/

/MyEclipse 8.5//mytest//src//com//RandomAccessFileTest.java","r");
System.out.println("初始位置:"+raf.getFilePointer());
//移动指针位置
raf.seek(100);
byte[]bbuf = new byte[1024];
int hasRead = 0;
while((hasRead=raf.read(bbuf))>0){
System.out.println(new String(bbuf,0,hasRead));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(raf!=null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}


3.RandomAccessFile的追加内容

public class APPendContent {
public static void main(String[] args) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile("C://Users//zhang_zhihui//Workspaces/

/MyEclipse 8.5//mytest//src//com//out.txt", "rw");
raf.seek(raf.length());
//z注意getBytes()的应用
raf.write("追加的内容/r/n".getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

为了追加内容,程序应该先将记录指针移到文件最后,然后再开始向文件中输出内容。
注意:它只能在文件的最后追加内容,而不能在中间加入内容,因为会覆盖后面的内容。如果想实现

任意点加入功能可以用下面方法:首先创建一个临时文件,用以保存被插入文件的插入点后面的内容

。程序先将文件中插入点后的内容读入临时文件中,接下来程序重新定位到插入点,将需要插入的内

容添加到文件后面,最后将临时文件的内容添加到文件后面,通过这个过程就可以指向文件、指定位

置插入内容。

public class InsertContent {
public static void  insert(String fileName,long pos,String insertContent) {
RandomAccessFile raf = null;
//创建一个临时的文件来保存插入点后的数据
try {
File temFile = File.createTempFile("tem", null);
FileOutputStream tmpOut = null;
FileInputStream temIn = null;
temFile.deleteOnExit();

raf = new  RandomAccessFile(fileName, "rw");
tmpOut = new FileOutputStream(temFile);
temIn = new FileInputStream(temFile);
raf.seek(pos);
// 下面的代码将插入点后的内容读入临时文件中保存
byte[] bbuf = new byte[64];
int hasRead = 0;
while((hasRead=raf.read(bbuf))>0){
//将读取的文件放在临时文件
tmpOut.write(bbuf, 0, hasRead);
}

//下面代码插入内容
raf.seek(pos);
//追加需要插入的内容
raf.write(insertContent.getBytes());
//追加临时文件的内容
while((hasRead=temIn.read(bbuf))>0){
raf.write(bbuf,0,hasRead);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public static void main(String[] args) {
insert("Insert.java", 100, "插入的难题哦");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: