------- android培训、java培训、期待与您交流! ----------
1.字节流
OutputStream:写
InputStream:读
用法和字符流相似
只是写入的只能是字节或是字节数据
fos.write("paddd".getBytes());//转成字节数组
因为不用转换,不用刷新
要关闭资源
fos.close();
字节流的特有方法,确定字节流文件的字节数组长度
fis.available();
byte[] buf = new byte[fis.available()];
字节流缓冲区
字节读取时会自动提升到int,写入时会强制byte.
字节数据有可能读到11111111,转成int就会是-1,因此要&255还是保留原数据。还不会是-1.
*/
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
{
//通过in对象读取硬盘上数据,并存储buf中
if(count==0)
{
count== in.read(buf);
if(count<0)
return -1;
pos = 0;
byte b = buf[pos];
count--;
pos++;
return b&255;
}
else if(count>0)
{
byte b = buf[pos];
count--;
pos++;
return b&0xff;
}
return -1;
}
public void myClose()throws IOException
{
in.close();
}
}
2,读取键盘录入
/*
System.out:对应标准输出设备:控制台
System.in:对应标准输入设备:键盘
*/
import java.io.*;
class ReadIn
{
public static void main(String[] args)
{
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();
if("over".equals(s))
break;
System.out.println(s.toUpperCase());
sb.delete(0,sb.length());
}
else
sb.append((char)ch);
}
}
}
readLine方法是字符流BufferedReader类中的方法,
键盘录入的read是字节流InputStream的方法。方法相类似。
InputStreamReader
字符体系
读取转换流//字节通向字符的桥梁
OutputStreamWriter
字节体系
字符通向字节的桥梁
键盘的最常见写法
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
/*
System.out:对应标准输出设备:控制台
System.in:对应标准输入设备:键盘
*/
import java.io.*;
class ReadIn
{
public static void main(String[] args)throws IOException
{
//获取键盘录入对象
InputStream in = System.in;
//将字节流对象转成字符流对象,用转换流InputStreamReader
InputStreamReader isr = new InputStreamReader(in);
//提高效率,字符串进行缓冲区技术高效操作使用BufferedReader
BufferedReader bufr = new BufferedReader(isr);
String line = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
System.out.println(line.toUpperCase());
}
bufr.close();
}
}
import java.io.*;
class WritOut
{
public static void main(String[] args)throws IOException
{
//获取键盘录入对象
InputStream in = System.in;
//将字节流对象转成字符流对象,用转换流InputStreamReader
InputStreamReader isr = new InputStreamReader(in);
//提高效率,字符串进行缓冲区技术高效操作使用BufferedReader
BufferedReader bufr = new BufferedReader(isr);
OutputStream out = System.out;
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bufw = new BufferedWriter(osw);
String line = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
//字符中要刷新
bufw.flush();
}
bufr.close();
}
}
3.流操作规律
使用流对象的基本规律
1明确源和目的
源:输入流InputStream和Reader
目的:输出流OutputStream和Writer
2操作的数据是否是纯文本
是:字符流
不是:字节流
3体系明确后,再明确要使用哪个具体对象,通过设备进行区分。
源设备:内存,硬盘,键盘
目的设备:内存,硬盘,控制台
转换流是字符和字节之间的桥梁,通常涉及到字符编码转换时,需要用到。
System.setIn:改变源,可以改成把文件变成输入
System.setOut:改变目的,可以改成输出到文件
io
util
text
catch(Exception e)
{
try
{
Date d = new Date();//获取时间
SimpleDateForma sdf = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");//时间格式化
String s = sdf.format(d);
PrintStream ps = new PrintStream("exception.txt");//设置异常存储文件
ps.println(s);
System.setOut(ps);//改变输出设置
}
catch (IOException ex)
{
throw new RuntimeException("日志文件创建失败");
}
e.printStackTrace(System.out);
}
也可以把系统信息变成文件