File f1 = new File(file);
FileInputStream fis = new FileInputStream(f1);
byte[] bytIn = new byte[(int) f1.length()];
for (int i = 0; i < f1.length(); i++) {
bytIn[i] = (byte) fis.read();
}这样读取速度太慢了,怎样提升读取速度
FileInputStream fis = new FileInputStream(f1);
byte[] bytIn = new byte[(int) f1.length()];
for (int i = 0; i < f1.length(); i++) {
bytIn[i] = (byte) fis.read();
}这样读取速度太慢了,怎样提升读取速度
解决方案 »
- swing如何播放视屏文件???
- java 中多个按钮怎么实现监听不用e.getSource()方法;
- vector
- 如何将oracle的备份文件(.dmp)转入到sql中?
- 怎样才可以关闭线程啊??
- 问个小问题:如何在退出菜单的事件处理中发出一个退出消息?
- 哪为高手请指教(最好能够用SQL文解决),高分赠送!
- 是否jdk1.3.1有bug,不信请执行下面的代码:怎么不好用那。另外告诉我怎么给分。我还不会。那位大虾幸运的话会得到我的100分
- 请问怎样用Appletviewer 浏览 APPLET ,谢谢
- 新手求教Eclipse使用的一个小问题
- common-fileupload 实现下载功能
- 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
FileInputStream fis = new FileInputStream(f1);
BufferedInputStream bf = new BufferedInputStream(fis,1024*10);
byte[] bytIn = new byte[(int) f1.length()];
for (int i = 0; i < f1.length(); i++) {
bytIn[i] = (byte) fis.read();
}
是不是写错了, bytIn[i] = (byte) fis.read(); 应该是 bf.read() 对吧
for(int i = 0; i<f1.length() ; i++)
这样写?
FileInputStream fis = new FileInputStream(f1);
InputStreamReader r = new InputStreamReader(fis,"utf-8");//文件采用的编码方式
BufferedReader bf = new BufferedReader(r,1024*10);
String s = null;
while((s =bf.readLine())!=null){
System.out.println(s);
}
import java.nio.*;
import java.nio.channels.*;public class FileOperation {
public FileOperation() {
}
/**
* @param filePath 要读取的文件
* @param position 读取开始位置
* @param length 读取长度
* @return 文件内容
*/
public String GetStrFromFile(String fileName, int position, int length) {
String strFromFile = "";
File f = new File(fileName);
if(f.exists()) {
try {
RandomAccessFile raf = new RandomAccessFile(fileName,"rw");
FileChannel fChan = raf.getChannel();
MappedByteBuffer buffer = fChan.map(FileChannel.MapMode.PRIVATE, position, length);
strFromFile = (new sun.misc.BASE64Encoder()).encodeBuffer(buffer);
fChan.close();
raf.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
}
return strFromFile;
}
}
public void copy(File newFile, File oldfile) {
try {
newFile.createNewFile();
FileInputStream fin = new FileInputStream(oldfile);
FileOutputStream fout = new FileOutputStream(newFile);
byte[] by = new byte[512];
int rs=-1;
while ( (rs=fin.read(by))>0) {
fout.write(by, 0, rs);
System.out.println(fin.read(by));
}
System.out.println("读取的信息:"+fin.read(by));
} catch (Exception e) {
e.printStackTrace();
}
}
以上函数中while()语句中相应位置换为以下语句:
int rs=-1;
rs=fin.read(by);
while ( rs>0) {
fout.write(by, 0, rs);
System.out.println(fin.read(by));
}
这种情况下为什么会出现死循环?
while ( rs>0) {
...
}while条件恒成立,所以会出现死循环。