java _io_文件读取标准步骤

File f =new File("~"); //创建源
InputStream is =new FileInputStream(f); //选择流
is.read() 读取单个数据,并使游标下移 //操作(读取)
is.close() //释放资源,输入流读取后必须释放资源

public class test{

public static void main(String[]args)
{
    //创建源
    File f=new File("C:/Users/10853/eclipse-workspace/hell/src/hell/abc");
    InputStream is =null;//提升is的作用域,避免在try中声明后,作用域
                        //只在try,finally中语句无法执行
    //选择流
    try {
         is =new FileInputStream(f);
    //操作(读取)
        int temp;
        while((temp=is.read())!=-1) //temp=is.read()表达式整体的值就是temp的值
        {                           //is.read()会读取单个数据,当数据读取完毕时,返回-1
            System.out.println((char)temp);
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally
    {
        try {
            if(null!=is)//当is创建成功时才执行关闭
            {
                is.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
}

原文地址:https://blog.51cto.com/14437184/2422845

时间: 2024-07-31 18:27:50

java _io_文件读取标准步骤的相关文章

java属性文件读取,属性修改

/** * 属性文件读取 * @author bestmata * */ public class CommUtil { private static Logger logger=Logger.getLogger(CommUtil.class); private Properties getAttionReplyPro(){ try { InputStream in=CommUtil.class.getResourceAsStream("attionReply.properties")

java程序文件读取与保存实例代码

1 class RadioHere extends JFrame implements ActionListener 2 { 3 private JTextArea ta=new JTextArea(10,20); 4 private JFileChooser jfc=new JFileChooser(new File(".")); 5 private JButton bOpen,bSave; 6 public RadioHere() 7 { 8 super("文档选择框应用

java _io_文件的拷贝,面向对象风格

//文件拷贝,以程序为中转站,从一个文件到另一个文件可处理图片和文本思路:type[] flush = new type[1024]在 .read(flush) ,时已将将内容存储到字节数组,只需再进行写出即可os.write(flush,0,len)然后刷新缓存,os.flush() public class test{ private String path; private String path2; private int len; private File first; private

java _io_随机读取读入流RandomAccessFile

随机读取和写入流RandomAccessFile 支持读取和写入随机访问文件 RandomAccessFile raf=new RandomAccessFile(文件对象,读写模式);r只读,rw读和写 private File f; //目的地 private String dir; //所有分割后的文件存储路径 private List<String> list; //每块大小 private int blockSize; //块数 private int size; public n(F

java _io_文件输出

1.创建源2.选择流3.操作(写出)4.刷新缓存,避免数据驻留内存5.释放资源 File f =new File("D:/d/t.txt"); //文件不存在stream流会自动创建OutputStream os=new FileOutputStream(f,true) //添加布尔类型true,会开启追加模式,默认为false.byte[] data =s.getBytes() //编码os.write(byte[] data) //写出字节数组的内容os.write(byte[]

java _io_文件字符流输入

字符流读取数据不会产生乱码问题字节流读取数据可能会因为字符集不同,每个字符对应的字符大小不同而产生乱码/ Read read= new FileReader(File f);或路径 操作方法:除了流的选择改变和字节数组变成了字符数组,其他都一样 public class test{ public static void main(String[]args) { File f =new File("C:/Users/10853/eclipse-workspace/hell/src/hell/abc

java _io_文件的拷贝、图片的拷贝、可变参数、try...with...resource面向

public static void close(Closeable... io) //可变参数,相当于数组 Closeable... io //可变参数,使用时相当于数组(for循环遍历)Closeable 输入输出流实现的接口,在释放资源时使用try...with..resource 自动释放资源工具:在try后接(is;os),其他的不变,会自动释放资源,不用管先后顺序(手动书写关闭时要先打开的后关闭)try(InputStream iso=is;OutputStream oso=os){

java _io_文件字符流输出

String s="ada";char[] a=s.toCharArray(); 将字符串转换成字符数组 public class test{ public static void main(String[]args) { File f =new File("D:d/write.txt"); Writer writer=null; try { writer=new FileWriter(f); //写法一 /*String s="adad afaff哈哈哈

java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

1 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.