I/O模式
来源:互联网 发布:python 爬虫多进程 编辑:程序博客网 时间:2024/06/14 13:44
文件:存储数据的一种方式;
File类
读文件
//读文件 try { FileInputStream fileInputStream= new FileInputStream("f:/Io/test.txt"); byte[] data = new byte[1024]; try { int len; while((len=fileInputStream.read(data))!=-1){ String string = new String(data,0,len); System.out.println(string); } fileInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
写文件
try { FileOutputStream fos = new FileOutputStream("f:/Io/abc.txt"); try { fos.write("abc".getBytes()); fos.flush(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
判断文件是否存在
if(!file3.exists()){ System.out.println("bucunzai");
创建文件
File file = new File("f:"+File.separator +"io"+File.separator+"old");//找到所指文件夹 if(!file.exists()){ file.mkdir();//如果该文件夹不存在则创建一个文件夹 } if (file.exists()) { File file2 = new File(file.getAbsolutePath() +File.separator+"old.txt");//如果存在所述文件夹,判断所述文件是否存在 if(!file2.exists()){ // 如果文件不存在 try { FileOutputStream fileOutputStream=new FileOutputStream(file2);//创建新的输出文件; try { fileOutputStream.write("hello file".getBytes()); fileOutputStream.flush(); fileOutputStream.close();//关闭输出流; }
删除文件
if(file.exists()){ file.delete(); } if (!file.exists()) { System.out.println("no....."); }
BufferReader
try { FileReader fReader = new FileReader("f:/Io/test.txt"); //流链 BufferedReader bReader = new BufferedReader(fReader);//高级流,提供了一个缓冲区; try { String string = null; while((string = bReader.readLine())!=null){ System.out.println(string); } fReader.close();//先关里面的 bReader.close();//再关外面的 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }//一次读一行 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
BufferWriter
FileWriter fWriter; try { fWriter = new FileWriter("f:/Io/test.txt"); BufferedWriter bWriter = new BufferedWriter(fWriter); bWriter.write("hello file"); bWriter.newLine(); bWriter.write("ok"); fWriter.flush(); bWriter.flush(); fWriter.close(); bWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
阅读全文
0 0
- I/O模式概述
- I/O调度模式
- WINSOCKS I/O模式
- I/O模式
- 套接字I/O模式
- 五种I/O 模式
- 五种I/O 模式
- 五种I/O 模式
- Java I/O装饰模式
- 五种I/O 模式
- 两种I/O模式
- 14.5 保护模式I/O
- 五种I/O 模式
- 五种I/O模式
- 5种I/O模式
- 14.5 保护模式I/O
- kvm I/O cache 模式
- 套接字I/O模式和I/O模型
- vim常用命令
- @RequestBody, @ResponseBody 注解
- HTTP协议简单总结
- Keras训练辅助工具及优化工具
- 自定义dialog,并且有Edittext,确定按钮获取Edittext的值
- I/O模式
- 上传文件
- 14:大象喝水
- C# WebApi 上传文件
- 2017暑假第二阶段第九场 总结
- 乘积最大子序列-LintCode
- 同步异步与阻塞非阻塞
- 【简趣学习文本挖掘】用Python带你走进神秘的“谍纸天眼”...
- DataTable搜索框、分页、页脚数据显示、显示数据多少的属性设置