当前位置: 首页 > news >正文

重庆实惠网站建设5118数据分析平台

重庆实惠网站建设,5118数据分析平台,网站开发的公司推荐,重庆网站建设报价文章目录File文件操作IO流处理流缓冲流转换流对象流File文件操作 利用File类来操作。 文件操作中常用到相对目录和绝对路径 package org.File; import java.io.File; public class demo01 { public static void main(String[] args) { try{ File file new File("…

文章目录

  • File文件操作
  • IO流
  • 处理流
    • 缓冲流
    • 转换流
    • 对象流

File文件操作

利用File类来操作。

文件操作中常用到相对目录和绝对路径

package org.File;  import java.io.File;  public class demo01 {  public static void main(String[] args) {  try{  File file = new File("./test.txt");  
//            文件路径是以项目目录为起点的。  
//            创建一个文件。  file.createNewFile();  }catch (Exception e ) {  e.printStackTrace();  }  }  
}

获取文件名

package org.File;  import java.io.File;  public class demo02 {  public static void main(String[] args) {  try {  File file = new File("src/main/java/org/File/Files/test.txt");  file.createNewFile();  
//            不能创建文件夹,创建的是文件  
//            System.out.println(file.getParentFile());//得到上一层文件夹的对象  
//            System.out.println(file.getParent());//得到上一层文件夹  
//            System.out.println(file.exists());//判断文件是否存在  file.mkdir();file.mkdirs();  
//            创建文件夹,一般选择mkdirs()  }catch (Exception e ){  e.printStackTrace();  }  }  
}

文件的所有相关操作

package org.File;  import java.io.File;  public class demo02 {  public static void main(String[] args) {  try {  File file = new File("src/main/java/org/File/Files/test.txt");  file.createNewFile();  
//            不能创建文件夹,创建的是文件  
//            System.out.println(file.getParentFile());//得到上一层文件夹的对象  
//            System.out.println(file.getParent());//得到上一层文件夹  
//            System.out.println(file.exists());//判断文件是否存在  file.mkdir();file.mkdirs();  
//            创建文件夹,一般选择mkdirs()  //            重命名  file.renameTo(new File("src/main/java/org/File/Files/test2.md"));  
//            删除文件  file.delete();  System.out.println(file.isAbsolute());  
//            判断是否是绝对路劲  
//            判断是不是文件夹  System.out.println(file.isDirectory());  System.out.println(file.isFile());  
//            判断是不是文件  System.out.println(file.length());  
//            查看文件大小  System.out.println(file.getName());  
//            文件名字。  }catch (Exception e ){  e.printStackTrace();  }  }  
}

创建文件的完整步骤

package org.File;  import java.io.File;  
import java.io.IOException;  
import java.text.FieldPosition;  public class demo03 {  public static void main(String[] args) {  File file = new File("resource/test.txt");  
//        1、判断上层文件夹是否存在  File parentFile = file.getParentFile();  if (!parentFile.exists())  {  parentFile.mkdirs();  }  
//        2、创建文件  try {  file.createNewFile();  } catch (IOException e) {  throw new RuntimeException(e);  }  }  
}

IO流

流的分类:

  • 按照读写的方向讲,是输入流和输出流
  • 按照读写单位分,字节流和字符流
  • 流的功能不同分,节点流和处理流

流的家族体系

输入输出
字节流InputStreamOutputStream
字符流ReaderWriter

在这里插入图片描述

节点流和处理流
节点流:直接连接在文件上的
处理流:套在其他流上的

文件流:
FileInputStream
FileOutputStream
FileReader
FileWriter

package org.FileStream;  import com.sun.org.apache.xpath.internal.operations.String;  import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  public class demo01 {  public static void main(String[] args) throws Exception {  //    创建流  FileInputStream fils = new FileInputStream(new File("./src/main/resources/a.txt"));  int read = fils.read();  System.out.println(read);  
//        输出115 s的Ascii码  System.out.println((char) read);  byte[] bs = new byte[1024];  int len = fils.read(bs );  //返回多少个字节  System.out.println(new String());  }  }

读取文件的一套写法

package org.FileStream;  import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  public class demo02 {  public static void main(String[] args) throws Exception {  FileInputStream files = new FileInputStream(new File("./src/main/resources/a.txt"));  byte[] bytes = new byte[1024];  int len=0;  while ((len= files.read(bytes))!=-1){  String s = new String(bytes, 0, len);  System.out.println(s);  }  
//        关闭文件流  files.close();  }  
}

写入文件:

public FileOutputStream(File file, boolean append)

append为true时是在文件后加入数据
默认是false,会清空文件,再添加数据。

package org.FileStream;  import com.sun.org.apache.xpath.internal.operations.String;  import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileReader;  public class demo04 {  public static void main(String[] args) throws Exception {  FileReader files = new FileReader(new File("./src/main/resources/a.txt"));  char[] cs = new char[1024];  int len = 0 ;  while ((len = files.read(cs))!=-1){  System.out.println(new String(cs,0 ,len));  }  files.close();  }  
}
package org.FileStream;  import java.io.File;  
import java.io.FileWriter;  
import java.io.IOException;  public class demo05 {  public static void main(String[] args) throws IOException {  FileWriter files = new FileWriter(new File("./src/main/resources/a.txt"),true);  files.write("simple");  files.flush();  files.close();  }  
}

将一张图片从一个地方复制到另一个地方

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d9QbRfYz-1676899099153)(../../images/Pasted%20image%2020230220192957.png)]
就将图片从a移动到b

package org.FileStream;  import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  public class demo06 {  public static void main(String[] args) throws Exception {  File f = new File("a/ba1.jpg");  FileInputStream fis = new FileInputStream(new File("a/ba1.jpg"));  FileOutputStream fos = new FileOutputStream(new File("b/bb1.jpg"));  
//        读取内容  byte[] bs = new byte[1024];  int len =0;  while ((len = fis.read(bs))!=-1){//读取数据  fos.write(bs,0,len);//写数据  }  fis.close();  fos.flush();  fos.close();  //        如果是剪切 ,就把原来的图片删掉就行了  
//        f.delete();  }  
}

在这里插入图片描述

处理流

缓冲流

带有一个缓冲区的数据流
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter

package org.Process_of_Stream;  import java.io.*;  public class demo01_Buffer_stream {  public static void main(String[] args) throws Exception {  
//        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("/src/main/resources/a.txt")));  
//        这里面套了三层东西,缓冲区套在节点流上,节点流套在文件流上  String path = Thread.currentThread().getContextClassLoader().getResource("a.txt").getPath();  System.out.println(path);  
//        获取路径  BufferedReader br = new BufferedReader(new FileReader(new File(path)));  System.out.println(br.readLine()); //读文本文件最好用的方法  System.out.println(br.readLine());  }  
}

标准的读取文件的写法

package org.Process_of_Stream;  import java.io.*;  public class demo01_Buffer_stream {  public static void main(String[] args) throws Exception {  
//        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("/src/main/resources/a.txt")));  
//        这里面套了三层东西,缓冲区套在节点流上,节点流套在文件流上  String path = Thread.currentThread().getContextClassLoader().getResource("a.txt").getPath();  System.out.println(path);  
//        获取路径  BufferedReader br = new BufferedReader(new FileReader(new File(path)));  //        System.out.println(br.readLine()); //读文本文件最好用的方法  
//        System.out.println(br.readLine());  String str ="";  while ((str=br.readLine())!=null ){  System.out.println(str);  }  }  
}

转换流

有时需要将流进行转换. 一般我们得到的会是一个字节流. 这是一般我们需要转换成字符流

java中有的转换:
字节流 --> 字符流
InputStreamReader
OutputStreamWriter

package org.Process_of_Stream;  import jdk.nashorn.internal.runtime.regexp.joni.exception.SyntaxException;  import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.util.Scanner;  public class demo02_Convert_stream {  public static void main(String[] args) throws Exception {  
//        系统输入是一个字节流  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
//        通过转换流,将字节流转换成了字符流  String s = br.readLine();  System.out.println(s);  }  
}
package org.Process_of_Stream;  import java.io.IOException;  
import java.io.OutputStreamWriter;  
import java.io.Writer;  public class demo03_Conver_stream {  public static void main(String[] args) throws IOException {  Writer writer = new OutputStreamWriter(System.out);  writer.write("hello simplecatlover");  writer.flush();  
//        writer.close();  //这个流不可以关  System.out.println("sgjagjla");  }  
}

对象流

也就是序列化和反序列化接口
两个类:
ObjectInputStream
ObjectOutputStream

类:
一定要implements Serializable

package org.Process_of_Stream.obj;  import java.io.Serializable;  public class Person implements Serializable {  private int id;  private String name;  private int age;  public Person(int id, String name, int age) {  this.id = id;  this.name = name;  this.age = age;  }  public void setId(int id) {  this.id = id;  }  public void setName(String name) {  this.name = name;  }  public void setAge(int age) {  this.age = age;  }  public int getId() {  return id;  }  public String getName() {  return name;  }  public int getAge() {  return age;  }  
}

序列化:

package org.Process_of_Stream.obj;  import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.ObjectOutputStream;  public class test1 {  public static void main(String[] args) throws Exception {  ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("person.dat")));  Person p = new Person(1,"simple",2);  oos.writeObject(p);  oos.flush();  oos.close();  }  
}

反序列化:


package org.Process_of_Stream.obj;  import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.ObjectOutputStream;  public class test1 {  public static void main(String[] args) throws Exception {  ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("person.dat")));  Person p = new Person(1,"simple",2);  oos.writeObject(p);  oos.flush();  oos.close();  }  
}
http://www.dt0577.cn/news/9802.html

相关文章:

  • 北京创意网站建设成都今天重大新闻事件
  • 西安网站建设阳建网络推广公司联系方式
  • 怎样做网站公司百度推广官方投诉电话
  • 公众号制作用什么软件seo对各类网站的作用
  • 网站运营的主要工作内容优化模型数学建模
  • 苏州高端网站设计建设网站如何推广运营
  • 石家庄电商网站建设杭州seo公司
  • 大型b2b电子商务平台开发孝感seo
  • wordpress建一个网站seo 0xu
  • 建设工程有限公司企业网站站长之家 seo查询
  • 凉山网站建设怎么联系百度人工客服
  • 惠州做网站广告惠州seo怎么做
  • 电商网站分析报告怎么做网站排名大全
  • 深圳龙华做网站的网络营销有哪些模式
  • 外贸公司网站案例厦门网
  • 吉林省政府网站建设搜狗网
  • 网站源码文件安装教程sem竞价外包公司
  • 如何用ps做网站首页秦皇岛网站seo
  • 华夏望子成龙网站开发背景今天的新闻是什么
  • 网站伪静态规则怎么找精准客户资源
  • 科技公司内蒙古网站制作seo做得比较好的公司
  • 网站建设公司在哪里长沙做网站推广公司咨询
  • 怎么做提卡密网站seo研究中心道一老师
  • 做画册的网站seo搜索引擎优化是
  • 国产做爰网站磁力多多
  • 氧os哪个网站做的最好抖音排名优化
  • 在线获取颜色代码网站seo外链发布平台有哪些
  • 网站开发网站设计的标准谷歌浏览器下载app
  • 南通seo网站推广费用bing收录提交
  • 网站数据库太大搬家还原500错误百度信息流效果怎么样