美文网首页
java的io简单操作

java的io简单操作

作者: Bruce杨 | 来源:发表于2017-02-15 10:35 被阅读0次
    import java.io.*;
    /**
     * Created by bruce on 2017/2/15.
     */
    public class Test {
        public static void main(String[] args) {
            File file = createNewFile();
            String content = "hello world";
    //        writeStrToFile(file, content);
            String readStr = readStrFromFile(file);
            System.out.println(readStr);
        }
    
        /**
         * 从file读取字符串
         *
         * @param file
         * @return
         */
        private static String readStrFromFile(File file) {
            String content = null;
            if (file.exists()) {
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(file);
                    //创建一个长度为1024的竹筒
                    byte[] bbuf = new byte[1024];
                    //用于保存实际读取的字节数
                    int hasRead = 0;
                    //使用循环来重复“取水”的过程
                    while ((hasRead = fileInputStream.read(bbuf)) > 0) {
                        //取出"竹筒"中(字节),将字节数组转成字符串输入
                        content = new String(bbuf, 0, hasRead);
                        System.out.println(content);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                new Throwable("文件不存在");
            }
            return content;
        }
    
        /**
         * 在本地创建一个txt文件
         *
         * @return
         */
        private static File createNewFile() {
            File file = new File("e:\\java", "yang.txt");
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return file;
        }
    
        /**
         * 往文件里面写入字符串
         *
         * @param file
         * @param content
         */
        private static void writeStrToFile(File file, String content) {
            byte[] bytes = null;
            //建立输出字节流
            FileOutputStream fos = null;
            try {
                bytes = content.getBytes();
                fos = new FileOutputStream(file);
                //用FileOutputStream 的write方法写入字节数组
                fos.write(bytes);
                System.out.println("写入成功");
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                //为了节省IO流的开销,需要关闭
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:java的io简单操作

          本文链接:https://www.haomeiwen.com/subject/euxqwttx.html