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

    知识点总结(Java IO):

    操作文件的类--File

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

    基础操作代码:
    package test;
    import java.io.File;
    import java.io.IOException;
    public class FileDemo{
        public static void main (String args[]){
             File f = new File("d:" + File.separator + "test.txt");        //文件路径
             if(f.exists()){                                               //判断文件是否存在
                f.delete();                                                //删除文件
            }else{
                try{
                    f.createNewFile();                            //创建文件
                }catch(IOException)
            }
        }
    }
    
    字节流基本操作

    一,字节流

    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 :字符输入流

    三,转换流OutputStreamWriter类与IntputStreamReader类

    OutputStreamWriter类是Writer的子类将输出的字符流变为字节流,即将一个字符流的输出对象变为字节流的输出对象。如下

    Writer w=new OutputStreamWriter(new FileOutputStream(f));
    

    IntputStreamReader类是Reader的子类将输入的字节流变为字符流,即将一个字节流的输入对象变为字符流的输入对象

    Reader r=new InputStreamReader(new FileInputStream(f));
    

    内存操作流程:

    字节内存流:
    ByteArrayInputStream(内存字节输入流);
    ByteArrayOutputStream(内存字节输出流);

    字符内存流:
    CharArrayReader(内存字节输入流);
    CharArrayWriter(内存字节输出流)

    首先是要声明内存的输入输出流,再去进行操作。

    四,管道流:

    主要作用是可以进行两个线程间的通信

    管道输出流:PipedOutputSream;
    管道输入流:PipedInputStream
    用connect()方法来连接输出和输入流

    五,打印流PrintStream

    打印流进行格式化和C语言差不多

    六,System类

    1)System.out 向显示器上输出,是PrintStream的对象,PrintStream是OutputStream的字类,所以可以使用OutputStream向屏幕输出
    2)Syatem.err 错误信息输出
    3)System.in 键盘的输入流,是InputStream的对象

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

    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("d:" + File.separator + "test.txt");
            InputStream ip = new FileInputStream(f);
            byte a[] = new byte[(int) f.length()];
            for(int i = 0; i < f.length(); i++) {
                a[i] = (byte)ip.read();
                if(i%2 == 0 && (a[i] >= 'a' && b[i] <= 'z')) {
                    a[i] = (byte) (a[i] - 32);
                }
            }
            
            ip.close();
            System.out.println(new String(a));
        }
    }
  • 相关阅读:
    IdTCP的C++Builder2010示例(转)
    BCB实现BMP图片的RGB分解(转)
    c++ builder 2009如何生成独立运行exe(转)
    用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台)(转)
    灰度变换——反转,对数变换,伽马变换,灰度拉伸,灰度切割,位图切割
    XS128超声波程序
    兔子--ps中的基本工具总结(ps cs5)
    UML中的用例图
    hdu5400Arithmetic Sequence
    自己定义控件的onMeasure方法具体解释
  • 原文地址:https://www.cnblogs.com/yuanqizhizhi/p/11785955.html
Copyright © 2011-2022 走看看