62,625
社区成员
发帖
与我相关
我的任务
分享 public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
StringBuffer path = Conver.nsb("c:/下载/1.mp3");
ArrayList<Integer> data = ReadData.readByte(path);
long end = System.currentTimeMillis();
System.out.println("读取结束! 用时: " + (end - start));
start = System.currentTimeMillis();
WriteData.writeByte(Conver.nsb("c:/下载/2.mp3"), data);
end = System.currentTimeMillis();
System.out.println("写入结束! 用时: " + (end - start));
}
public static ArrayList<Integer> readByte(StringBuffer path) {
if (path == null || path.length() == 0) {
return null;
}
ArrayList<Integer> data = null;
LinkedList<Integer> tempData = new LinkedList<Integer>();
byte[] buff = new byte[1024];
FileInputStream fis = null;
try {
fis = new FileInputStream(path.toString());
}
catch (IOException e) {
e.printStackTrace();
}
for (int i = 0;;i += 1024) {
try {
if (fis.read(buff) == -1) {
break;
}
tempData.addAll(Conver.byteArrayToArrayInteger(buff));
}
catch (IOException e) {
e.printStackTrace();
}
}
try {
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
data = new ArrayList<Integer>(tempData);
return data;
} public static void writeByte(StringBuffer path, ArrayList<Integer> data) {
if (path == null || path.length() == 0) {
return;
}
if (data == null || data.size() == 0) {
return;
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path.toString(), true);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
for (int i = 0;i < data.size();++i) {
try {
fos.write(data.get(i));
}
catch (IOException e) {
e.printStackTrace();
}
}
// 关闭文件
try {
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
} public static ArrayList<Integer> byteArrayToArrayInteger(byte[] byteArray) {
if (byteArray == null || byteArray.length == 0) {
return null;
}
LinkedList<Integer> temp = new LinkedList<Integer>(); // 使用 LinkedList 添加数据
int number = 0;
for (int i = 0;i < byteArray.length;++i) {
number = byteArray[i];
temp.add(number);
}
// 将 LinkedList 转换为 ArrayList
ArrayList<Integer> data = new ArrayList<Integer>(temp);
return data;
}

File f = new File("文件路径");
File f2 = new File("文件路径");
InsputStream is = new FileInputStream(f);
OutputStream os = new FileOutPutStream(f2);
try{
byte[] byte = new byte[4096];
int n = 0;
while(-1!=(n=is.read(byte))){
os.write(byte, 0, n);
}
is.close();
os.close();
}catch(Exception e){
}[/quote]
边读边写当然很简单,我要将读出来的字节数据保存在变量里,然后给另一个方法写进去。File f = new File("文件路径");
File f2 = new File("文件路径");
InsputStream is = new FileInputStream(f);
OutputStream os = new FileOutPutStream(f2);
try{
byte[] byte = new byte[4096];
int n = 0;
while(-1!=(n=is.read(byte))){
os.write(byte, 0, n);
}
is.close();
os.close();
}catch(Exception e){
}[/quote]
不能混在一起,我要写一个方法专门用来读,另一个专门用来写File f = new File("文件路径");
File f2 = new File("文件路径");
InsputStream is = new FileInputStream(f);
OutputStream os = new FileOutPutStream(f2);
try{
byte[] byte = new byte[4096];
int n = 0;
while(-1!=(n=is.read(byte))){
os.write(byte, 0, n);
}
is.close();
os.close();
}catch(Exception e){
}
//org.apache.commons.io
FileUtils.copyFile(new File("c:/下载/1.mp3"),new File("c:/下载/2.mp3"));
这样最快,直接用工具,不用自己写