62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
class abc
{
public static void main(String[] args) throws IOException
{
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("d:\\02.jpg");
fos = new FileOutputStream("d:\\00000000.jpg");
byte[] a = new byte[1024*4];
int ch;
while((ch=fis.read(a))!=-1)
{
fos.write(a,0,ch);
}
}catch(Exception e){
}finally{
try{fis.close();}catch(Exception e){}
try{fos.close();}catch(Exception e){}
}
}
}
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Created by puruidong on 2015/5/1.
*/
public class Entity {
public static void main(String[] args) throws IOException {
if (!Files.exists(Paths.get(".\\test.txt"))) Files.write(Paths.get(".\\test.txt"), new byte[0]);
if (!Files.exists(Paths.get(".\\newtest.txt"))) Files.write(Paths.get(".\\newtest.txt"), new byte[0]);
try (
FileInputStream fis = new FileInputStream(".\\test.txt");
FileOutputStream fos = new FileOutputStream(".\\newtest.txt");
) {
if (Files.readAllLines(Paths.get(".\\test.txt")).size() == 0) {
Files.write(Paths.get(".\\test.txt"), ("first write------" + System.currentTimeMillis()).getBytes());
}else{
//一行代码写入文件.
Files.write(Paths.get(".\\test.txt"), ("more write------" + System.currentTimeMillis()).getBytes());
}
int result = 0;
byte[] by = new byte[1024];
while ((result = fis.read(by)) != -1) {
fos.write((new String(by) + "---" + System.currentTimeMillis()).getBytes());
}
/*
* java 8 一行代码读取文件内容
*
1)文件可能很大,可能会超出内存空间,使用前要做评估。
2)要输出日志,记录为什么无法读取文件或者在阅读文件时遇到的任何错误。
3)在把字节转换成字符时,应该指定字符编码。
4)要处理文件不存在的情况。
还要注意,如果读入的文件的编码是ANSI编码,那么上面的例子在读取文件内容时会报java.nio.charset.MalformedInputException: Input length = 1错误。
*
*/
Files.readAllLines(Paths.get(".\\newtest.txt"), Charset.defaultCharset()).forEach(t -> System.out.println("newtest.txt:" + t));
}
}
}