zoukankan      html  css  js  c++  java
  • 第十周课程总结

    本周学习了javaIO。
    File是唯一表示与文件本身有关的类,使用它可以进行创建或删除文件等操作。
    File类的构造方法:
    public File(String pathname) -->实例化File的时候,必须设计好路径。
    public static final String separator (Windows中表示路径的分隔符 "")

    字节流

    1.字节输出流:OutputStream
    OutputStream是整个IO包中字节输出溜的最大父类,此类的定义如下:
    public abstract class OutputStream
    extends Object
    implements Closeable, Flushable

    OutputStream类中常用的方法
    1.public void close() throws IOException 关闭输出流
    2.public void flush() throws IOException 刷新缓冲区
    3.public void write(byte[] b) throws IOEception 将一个byte数组写入数据流
    4.public void write (byte[] b, int off, int len) throws IOException 将一个指定范围的byte数组写入数据流
    5.public abstract void write (int b)throws IOEXception 将一个字节流数据写入数据流

    在写入操作中,重新执行程序,则肯定会覆盖文件中的已有内容,此时可以通过File Output Stream类中的另一种构造方法进行实例化,向文件中追加内容:
    public FileOutputStream(File file, boolean append)throws FileNotFoundException
    在此构造方法中,将append的值设置为true,则表示在文件末尾追加新内容。

    2.字节输出流 :InputStream
    定义:
    public abstract class InputStream
    extends Object
    implements Closeable

    InputStream类的常用方法
    1.public int available() throws IOException 取得输入文件的大小
    2.public void close() throws IOException 关闭输入流
    3.public abstract int read() throws IOEception 读取内容,以数字的方式读取
    4.public int read ( byte[] b) throws IOException 将内容读到byte数组之中,同时访问读入的个数

    字符流和字节溜差不多
    Writer: 字符输出流
    Reader :字符输入流

    转换流

    1.OutputStreamWriter : 是Writer的子类,将输出的字符流变为字节流,即将一个字符流的输出对象变为字节流输出对象
    2.InputStreamReader : 是Reader的子类,将输入的字节流变为字符流,即将一个字节流的输入对象变为字符流的输入对象

    内存操作流

    1.字节内存流:ByteArrayInputStream(内存字节输入流)丶ArrayOutputStream(内存字节输出流)
    2.字符内存流:CharArrayReader(内存字符输入流)丶CharArrayWriter(内存字符输出流)

    ByteArrayInputStream的功能主要是完成将内容写入到内存中,而ByteArrayOutputStream的功能主要是将内存中的数据输出

    ByteArrayInputStream类的主要方法
    1.public ByteArrayInputStream(byte[] bf) 将全部的内容写入内存中
    2.public ByteArrayInputStream(byte[] buf, int offset, int length) 将指定范围的内容写入到内存之中

    ArrayOutputStream类的主要方法
    ArrayOutputStream 创建对象
    public void wtrite(int b) 将内容从内存中输出

    管道流

    PipedOutoputStream :管道输出流
    PipedInputStream:管道输入流
    链接管道:
    public void connect (PipedInputStream snk) throws IOException

    问题:字节流输出时,奇数位小写,偶数位大写

    package test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    
    public class Ten {
    	public static void main (String args[]) throws Exception {
    		File f = new File("e:" + File.separator + "test.txt");
    		InputStream ip = new FileInputStream(f);
    		byte b[] = new byte[(int) f.length()];
    		for(int i = 0; i < f.length(); i++) {
    			b[i] = (byte)ip.read();
    			if(i%2 != 0 && (b[i] >= 'a' && b[i] <= 'z')) {
    				b[i] = (byte) (b[i] - 32);
    			}
    		}
    			
    		ip.close();
    		System.out.println(new String(b));
    	}
    }
    

  • 相关阅读:
    mysql查询版本
    mysql导出bug备注
    linux查找文件
    linux 编译式安装nginx
    linux文件校验
    linux 编译式安装apache
    网络层
    数据链路层
    物理层
    servlet过滤器
  • 原文地址:https://www.cnblogs.com/20188382llz/p/11785355.html
Copyright © 2011-2022 走看看