FileOutputStream 从流中输出 对应的是FileReader 将流中的字符读取
FileInputStream 输入到流里 对应的是 FileWriter 将字符写入到流
import java.io.*;
class OutputDemo {
public static void main(String[] args) throws IOException {
// writeFile();
readFile_3();
}
public static void writeFile() throws IOException {
FileOutputStream fs = new FileOutputStream("demo.txt");
fs.write("sazaz".getBytes());
fs.close();
}
public static void readFile() throws IOException {
FileInputStream fis = new FileInputStream("demo.txt");
int ch = 0;
while ((ch = fis.read()) != -1) {
System.out.println((char)ch);
}
fis.close();
}
public static void readFile_2() throws IOException {
FileInputStream fis = new FileInputStream("demo.txt");
byte[] buf = new byte[1024];
int num = 0;
while((num = fis.read(buf))!=-1) {
System.out.println(new String (buf,0,num));
}
fis.close();
}
public static void readFile_3() throws IOException {
FileInputStream fis = new FileInputStream("demo.txt");
byte[] buf = new byte[fis.available()]; //定义一个长度刚好的数组,会内存溢出
fis.read(buf);
System.out.println(new String(buf));
fis.close();
}
}
复制图片
import java.io.*;
class CopyDemo {
public static void main(String[] args) {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream("timg.jpg");
fos = new FileOutputStream("zz.jpg");
byte[] buf = new byte[1024];
int num = 0;
while((num = fis.read(buf)) !=-1) {
fos.write(buf,0,num);
}
}
catch(IOException e) {
throw new RuntimeException("shibai");
}
finally {
if(fis != null)
try {
fis.close();
}
catch(IOException e) {
throw new RuntimeException("shibai");
}
if(fos != null)
try {
fos.close();
}
catch(IOException e) {
throw new RuntimeException("shibai");
}
}
}
}
自定义缓冲区
import java.io.*;
class Demo {
public static void main(String[] args) throws IOException {
copy();
}
public static void copy() throws IOException {
MyBufferedInputStream bis = new MyBufferedInputStream (new FileInputStream("demo.java"));
BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream("xxx2.txt"));
int by = 0;
while ((by=bis.myRead()) != -1 ) {
bos.write(by);
}
bis.myClose();
bos.close();
}
}
import java.io.*;
class MyBufferedInputStream {
private InputStream in;
private byte[] buf = new byte[1024];
private int pos = 0,count = 0;
MyBufferedInputStream (InputStream in) {
this.in = in;
}
public int myRead() throws IOException { //在数组中读一个数据
if (count ==0) { //当count为0 时,说明缓冲区内数组被取光 将数据重新填入
if(count<0)
return -1;
count = in.read(buf); //将数据填入缓冲区的数组内
pos = 0;
byte b = buf[pos]; //在缓冲区内数组取角标为pos的数据
count--;
pos++;
return b;
}
else if (count > 0) {
byte b = buf[pos];
count--;
pos++;
return b;
}
return -1;
}
public void myClose() throws IOException {
in.close();
}
}
转换流
import java.io.*;
class ReadIn {
public static void main(String[] args) throws IOException {
InputStream in =System.in;
StringBuilder sb = new StringBuilder();
while(true) {
int ch = in.read(); //读取一个字符
if(ch=='\r')
continue;
if(ch=='\n') { //判断是否是换行符
String s = sb.toString(); //将sb中的数据给 String s
if("over".equals(s)) //判断“over”与s是否相同
break;
System.out.println(s); //将s输出到控制台
sb.delete(0,sb.length()); //将 sb清空
}
else
sb.append((char)ch); //往sb中添加ch
}
}
}
2
import java.io.*;
class ReadIn2 {
public static void main(String[] args) throws IOException {
InputStream in = System.in;
InputStreamReader isr = new InputStreamReader(in); //输入转换流
BufferedReader br = new BufferedReader(isr); //使用增强类可以方便读取一行
OutputStream out = System.out;
OutputStreamWriter osw = new OutputStreamWriter(out); //输出转换流
BufferedWriter bw = new BufferedWriter(osw); //使用增强类可以方便换行
//简易写法
// BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(Systen.out));
String s = null;
while((s = br.readLine()) !=null) {
if("over".equals(s))
break;
bw.write(s);
bw.newLine();
bw.flush(); //刷新流
}
br.close();
}
}
3
import java.io.*;
class ReadIn3 {
public static void main(String[] args) throws IOException {
print();
//往文件里面输入
BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("zz.txt")));
String s = null;
while((s = br.readLine()) !=null) {
if("over".equals(s))
break;
bw.write(s);
bw.newLine();
bw.flush(); //刷新流
}
br.close();
}
//将文件里的数据输出
public static void print() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader (new FileInputStream ("zz.txt")));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String s = null;
while((s = br.readLine()) !=null) {
if("over".equals(s))
break;
bw.write(s);
bw.newLine();
bw.flush(); //刷新流
}
br.close();
}
}
日志信息
import java.io.*;
import java.util.*;
import java.text.*;
class ExceptionDemo {
public static void main(String[] args) {
try {
int[] arr = new int[2];
System.out.println(arr[3]);
}
catch (Exception e) {
try {
Date d = new Date (); //创建日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d); //格式化日期
PrintStream ps = new PrintStream ("zzzzzzz.log"); //创建目录文件
ps.println(s);
System.setOut(ps); //指定默认存储位置
}
catch (IOException ex) {
throw new RuntimeException("创建失败");
}
e.printStackTrace(System.out); //
}
}
}