两个方法:
read();
read(byte[] buf);
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author cuijiao
*
*/
public class IOStream {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File("E:\\data\\logs\\homestay-error.log.2018-05-02");
FileInputStream fis = new FileInputStream(file);
int asc = -1;
while ((asc = fis.read()) != -1) {
System.out.print((char) asc);
}
fis.close();
}
}
一次性读多个字节,byte[]作为缓冲区暂存
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author cuijiao
*
*/
public class Filestream {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("E:\\data\\logs\\udd-config-admin-error.log");
FileInputStream fis = null;
int lenth = -1;
try {
fis = new FileInputStream(file);
byte[] buf = new byte[1024];
lenth = fis.read(buf);
while (lenth != -1) {
System.out.println(new String(buf, 0, lenth));
lenth = fis.read(buf);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
fis = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream写:内存——文件
write(byte[] b)//清空重写
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/**
* @author cuijiao
*
*/
public class Filestream {
public static void main(String[] args) {
File file = new File("D:\\data\\test.log");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner sc = new Scanner(System.in);
System.out.println("请输入要写入文件的内容");
String str = sc.nextLine();
byte[] b = str.getBytes();
try {
fos.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (sc != null) {
sc.close();
sc = null;
}
if (fos != null) {
fos.close();
fos = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author cuijiao
*
*/
public class Filestream {
public static void main(String[] args) throws IOException {
File src = new File("D:\\data\\test.jpg");
File target = new File("E:\\copy.jpg");
copyFile(src, target);
}
/**
* 拷贝文件:将src复制到target
*
* @param src
* @param target
* @throws IOException
*/
private static void copyFile(File src, File target) throws IOException {
if (!src.exists()) {
return;
}
if (!target.exists()) {
target.createNewFile();
}
FileInputStream fis = null;
FileOutputStream fos = null;
fis = new FileInputStream(src);
fos = new FileOutputStream(target);
byte[] buf = new byte[1024];
int lenth = -1;
lenth = fis.read(buf);
while (lenth != -1) {
fos.write(buf, 0, lenth);
lenth = fis.read(buf);
}
fis.close();
fos.close();
}
}