File I/O
来源:互联网 发布:ewb仿真软件下载 编辑:程序博客网 时间:2024/06/06 03:30
1. 字节流
public static void main(String[] args) throws IOException {// TODO Auto-generated method stub File file=new File("E:/S2226.txt"); //创建文件 file.createNewFile();if(file.exists()){System.out.println("存在"); //判断是否存在}long lastModified = file.lastModified();//文件最后一次修改时间 对比时间1970年 1月1日 输出数字为毫秒System.out.println(lastModified);System.out.println(file.length());}
public static void main(String[] args) throws IOException {// TODO Auto-generated method stub FileInputStream fis=new FileInputStream("E:/S2226.txt"); byte[] bytes=new byte[1024]; int data=fis.read(bytes); while(data!=-1){ String temp=new String(bytes,0,data); System.out.println(temp); data=fis.read(bytes); }
记得关闭流
输出的效果图 如下:
这里要关闭流.
2. 字符流
public static void main(String[] args)throws IOException {// TODO Auto-generated method stub//读取文件Reader reader=new FileReader("E:/S2226.txt");BufferedReader bf=new BufferedReader(reader);String line;while ((line=bf.readLine())!=null){System.out.println(line);}bf.close();reader.close();}
public static void main(String[] args)throws IOException {// TODO Auto-generated method stub Writer writer=new FileWriter("E:/S2226.txt"); BufferedWriter bw=new BufferedWriter(writer); String words="嗯哼?"; bw.write(words); bw.close(); writer.close(); }
public static void main(String[] args) throws IOException{// TODO Auto-generated method stubInputStream is=new FileInputStream("D:/001.gif");DataInputStream dis=new DataInputStream(is);OutputStream os=new FileOutputStream("E:/002.gif");DataOutputStream dos=new DataOutputStream(os);byte[] bytes=new byte[1024];int data;while((data=dis.read(bytes))!=-1){dos.write(bytes,0,data);}dos.close();os.close();dis.close();is.close();System.out.println("粘贴成功");}
0 0
- linux file I/O
- Linux file I/O
- C++ File I/O
- FILE I/O
- Multithreaded File I/O
- FILE I/O
- Python File I/O
- I/O 1.0 File
- File I/O
- C++ File I/O
- I/O----File类
- Python File I/O
- Perl - File I/O
- 3-File I\O
- Python File I/O
- 【JAVA】File I/O
- FILE I/O 错误
- File I/O
- Android恢复出厂设置
- 程序员面试100题之四:求1+2+...+n
- CI PHP7 session 不能读取的问题
- SSM框架——使用MyBatis Generator自动创建代码
- html5+go+websocket简单实例代码
- File I/O
- yii2 添加 自定义 组件 custom component,以及模块 module 原理的详解剖析
- Android应用开发编译框架流程与IDE及Gradle概要
- 《C++ primer》5th读书笔记(chapter3)
- 解决tableViewCell分割线不到左边界的问题
- C++ error: unknown type name
- 一.makefile 基础认识
- 用结构化思维策划一个会议
- jQuery解除事件绑定 unbind