字节流与字符流 与其常用操作

字节流与字符流

字节流
占一个字节,8位。
用于:视频、图像、音频等类容
IntputStream输入流 (OutputStream输出流)
//都可以向上转型
InputStream子类 数据源类型
ByteArrayInputStream 包含一个内存缓冲区,字节从中取出。
FileInputStream 从文件中获得字节。 ObjectInputStream 用来恢复被序列化的对象。
PipedInputStream 管道输入流,读取管道内容。多和PipedOutputStream一起用于多线程通信。
SequenceInputStream 是多种输入流的逻辑串联,从第一个输入流读取,直到最后一个输入流。
StringBufferInputStream 读取的字节由字符串提供。

方法 解释
available() 返回此输入流下一个方法可以读取的字节数。
close() 关闭此输入流并释放相应资源。
mark(int) 在此输入流中标记当前的位置。
markSupported() 测试此输入流是否支持mark和reset方法。
read() 从此输入流中读取下一个字节(此方法是抽象方法,子类必须实现该方法)。
read(byte[]) 从输入流中读取一定数量的字节,存储在参数指定的字节数组中。
read(byte[],int,int)从输入流中指定位置起读取若干字节存储在指定字节数组中。
reset() 将此输入流定位到最后一次mark的位置。
skip(long) 跳过和丢弃此输入流中数据的若干字节。

字符流
Writer写入流 和Reader读取流
占2个字节,16位
用途:用于文本

其子类:
BufferedWriter
CharArray
FilterWriter
OutputStreamWriter->FileWriter
PipedWriter
PrintWriter
StringWriter

将流转化成二进制类型
public byte[] ImageGetByte(InputStream inputStream) throws Exception
{
ByteArrayOutputStream outp = new ByteArrayOutputStream();
int ch = 0;
while( (ch=inputStream.read())!=-1 )
outp.write(ch);
byte[] b = outp.toByteArray();
return b;
}

将字节流转化成字符串string
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, “utf-8”));
StringBuffer buffer = new StringBuffer();
String line = “”;
while ((line = in.readLine()) != null){
buffer.append(line);
}
String str = buffer.toString();

文件写入流

File file =new File(“”);
FileInputStream fileInputStream =new FileInputStream(file);
InputStream inputStream = (InputStream)fileInputStream; //向上转换

将流写入文件
方法一:
InputStream inputStream=connection.getInputStream();
byte[] b=new getByte().ImageGetByte(inputStream);
File file =new File(“C://Users/luo/Desktop/img”+”1.jpg”);
FileOutputStream fileint = new FileOutputStream(file);
for (byte a : b) {
fileint.write(a);
}
System.out.println(“读取成功”);
fileint.close();

方法二(区别是缓存的方式) URL获取资源
URL url = new URL(“http://47.94.101.75:8080/PhoneLibrarya/1.jpg“);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
String imageName = “C://Users/luo/Desktop/img/1.jpg”; //写入目标地址
FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));

byte[] buffer = new byte[1024];
int length;

while ((length = dataInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}
dataInputStream.close();
fileOutputStream.close();

往文件里写内容

FileWriter file=new FileWriter(path);
PrintWriter pw=new PrintWriter(file);
(1)、write():仅支持输出字符类型数据,字符、字符数组、字符串等
(2)、print():可以将各种类型(包括Object)的数据通过默认编码转换成bytes字节形式,这些字节都通过write(int c)方法被输出
pw.printf()
pw.print()
pw.write()

File f=new File(path);
if(f.isFile() && f.exists())
{
return path;
}
else
{
//不存在则写入
//来源
InputStream input=b.getBinaryStream();
//出口
OutputStream output=new FileOutputStream(f);
//创建缓冲区
byte[] buff=new byte[input.available()];
input.read(buff);//客人进
output.write(buff);//客人出
input.close();
output.close();
return path;
}

文件路径
在纯java类中
String dirpath = System.getProperty(“user.dir”);
String xmlFile = dirpath + “/WebRoot/WEB-INF/server.xml”;
String fileName = dirPath + “/server.xml”;

在servlet中
String dirPath = getServletContext().getRealPath( “/WEB-INF”);
String xmlFile = dirPath + “/server.xml”;

在jsp中
String dirPath= request.getServletContext().getRealPath(“/WEB-INF”);
String xmlFile = dirPath+”server.xml”;

设置时钟

   private String DEFAULT_TIME_FORMAT = "yyyy-MM-dd hh:mm:ss"; 
   private String time; 
   private int ONE_SECOND = 1000;
   private int ONE_minute =60000;

            public void configTimeArea() { 
           Timer tmr = new Timer(); 
           tmr.scheduleAtFixedRate(new JLabelTimerTask(), new Date(), ONE_SECOND); 
       } 

       protected class JLabelTimerTask extends TimerTask { 
           SimpleDateFormat dateFormatter = new SimpleDateFormat( 
                   DEFAULT_TIME_FORMAT); 

           @Override 
           public void run() { 
               time = dateFormatter.format(Calendar.getInstance().getTime()); 

//这里设置显示时钟
}
}

猜你喜欢

转载自blog.csdn.net/weixin_40990818/article/details/81561484