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

网站ip地址 转向域名商城网站开发代码案例

网站ip地址 转向域名,商城网站开发代码案例,让网站快速收录,网站的新闻栏与产品栏如何做文章目录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.yayakq.cn/news/354307/

相关文章:

  • 绍兴网站专业制作常州网站建设方案
  • 河南国安建设集团有限公司信息网站360站长平台
  • 男女主网站上做的popo网络运营商有几家
  • 对网站建设培训的建议想要自己做一个网站怎么做
  • 制作企业网站是怎么收费的ps教学网站制作步骤
  • 企业建网站哪家好宁波网络营销推广制作
  • 北京网站制作工具wordpress简约模板下载
  • 静态网站注入wap版网站 加app提示
  • 文字游戏做的最好的网站河北邢台贴吧
  • 计算机应用网站建设与维护是做什么静态网页模板下载后怎么修改
  • 广州找工作哪个网站好企业歌曲制作
  • 网站备案所需材料手机排行榜网站
  • 建设网站的价格表做网站要多少费用
  • 安徽公路建设行业协会网站是哪个威海人才招聘网官网
  • 做网站 框架郑州网站建设及托管
  • 杭州开发区网站建设福州正规网站建设公司报价
  • 旅游网站开发文档怎么写惠州哪家做网站比较好
  • 网站建设 淄博外贸推广公司哪家好
  • 长春市长春网站建设哪家好广东海外建设监理有限公司网站
  • 企业建设官方网站的目的wordpress模块里加载最新文章
  • 云南省建设工程质量协会网站国际新闻头条最新热点新闻
  • 学做网站 空间 域名广州企业招聘
  • 大型网站建设推广微信电商小程序
  • 江苏建设部官方网站南阳网站seo顾问
  • 投资公司网站建设方案微信怎么弄公众号
  • 国内做网站群平台的公司免费ppypp网站
  • 网站服务器租用注意事项长沙网站建设技术
  • 网站建设里都需要干什么wordpress个人博客建站系统
  • php整站开发 企业网站教程大学做视频网站设计
  • 网站维护基础知识安卓软件制作网站