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

重庆建设局网站长春网站如何制作

重庆建设局网站,长春网站如何制作,咚门wordpress,建立网站的正确方法文章目录一、补充二、File类File类的含义创建多级文件File类的常见方法三、IO流IO流分类输入输出流FileOutputStreamInputStreamInputStream与OutputStream的实例ReaderWriterFileReader和FileWriter的实例缓冲流转换流序列化与ObjectInputStream、ObjectOutputStream打印流Pro… 文章目录一、补充二、File类File类的含义创建多级文件File类的常见方法三、IO流IO流分类输入输出流FileOutputStreamInputStreamInputStream与OutputStream的实例ReaderWriterFileReader和FileWriter的实例缓冲流转换流序列化与ObjectInputStream、ObjectOutputStream打印流Properties参考文章Java IO流超详细参考文章文件操作参考文章最全最详细的IO流教程一、补充 1. 文件分隔符小知识 WindowsD:\Soft\QQ\PluginLinuxD:/Soft/QQ/Plugin 2. 换行 Windows换行 回车换行即\r\nLinux换行 换行即\n 3. IDEA默认的当前路径 工程Project的根就是IDEA的默认当前路径 4. IDEA文件分隔符格式 Windows File file new File(D:\test\test.txt);LinuxFile file new File(D://test//test.txt);统一File testFile new File(“D:” File.separator “test” File.separator fileName); 二、File类 File类的含义 File(String path)如果 path 是实际存在的路径则该 File 对象表示的是目录如果 path 是文件名则该 File 对象表示的是文件。 创建多级文件 例如D:\filepath\test\test.txt先创建多级目录D:\filepath\test在创建文件test.txt package com.zte.FileTest;import sun.net.ftp.FtpClient; //import com.zte.ums.cnms.pm.load.enums.MppFileDirEnum; import java.io.File; import java.io.IOException;public class FileTest {public static void main(String[] args) throws IOException {File testFile new File(D: File.separator filepath File.separator test File.separator test.txt);File fileParent testFile.getParentFile();//返回的是File类型,可以调用exsit()等方法 System.out.println(fileParent: fileParent);if (!fileParent.exists()) {fileParent.mkdirs();// 能创建多级目录}if (!testFile.exists())testFile.createNewFile();//有路径才能创建文件System.out.println(testFile);String path testFile.getPath();String absolutePath testFile.getAbsolutePath();String getFileName testFile.getName();System.out.println(path:path);System.out.println(absolutePath:absolutePath);System.out.println(getFileName:getFileName);} }输出结果 fileParent:D:\filepath\test D:\filepath\test\test.txt path:D:\filepath\test\test.txt absolutePath:D:\filepath\test\test.txt getFileName:test.txtFile类的常见方法 public class FileGet {public static void main(String[] args) {File f new File(d:/aaa/bbb.java); //文件绝对路径:d:\aaa\bbb.java System.out.println(文件绝对路径:f.getAbsolutePath());//文件构造路径:d:\aaa\bbb.javaSystem.out.println(文件构造路径:f.getPath());//文件名称bbb.javaSystem.out.println(文件名称:f.getName());//文件长度2116字节System.out.println(文件长度:f.length()字节);//是否存在trueSystem.out.println(f.exists());} }三、IO流 IO流分类 1. 字节流 按照 字节 的方式读取数据一次读取1个字节byte等同于一次读取8个二进制位。 这种流是万能的什么类型的文件都可以读取。包括文本文件图片声音文件视频文件 等 eg. 假设文件file1.txt采用字节流的话是这样读的 a中国bc张三fe 第一次读一个字节正好读到’a’ 第二次读一个字节正好读到’中’字符的一半。 第三次读一个字节正好读到’中’字符的另外一半。 2. 字符流 按照 字符 的方式读取数据的一次读取一个字符. 这种流是为了方便读取 普通文本文件 而存在的这种流不能读取图片、声音、视频等文件。只能读取 纯文本文件连word文件都无法读取。 注意 纯文本文件不单单是.txt文件还包括 .java、.ini、.py 。总之只要 能用记事本打开 的文件都是普通文本文件。 3. 字符流和字节流的爱恨情仇 字符流 字节流 编码表 输入输出流 FileOutputStream 1. 构造方法 public FileOutputStream(File file)以File对象为参数创建对象public FileOutputStream(String name)根据文件名称创建对象 // 使用File对象创建流对象 File file new File(G:\\自动创建的文件夹\\a.txt); FileOutputStream fos new FileOutputStream(file);// 使用文件名称创建流对象 FileOutputStream fos new FileOutputStream(G:\\b.txt);2. 实现数据追加 每次程序运行每次创建输出流对象都会清空目标文件中的数据。如何保留目标文件中数据还能继续追加新数据呢并且实现换行呢其实很简单这个时候我们又要再学习FileOutputStream的另外两个构造方法 public FileOutputStream(File file, boolean append)public FileOutputStream(String name, boolean append) public class FOSWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos new FileOutputStream(fos.txttrue); // 字符串转换为字节数组byte[] b abcde.getBytes();// 写出从索引2开始2个字节。索引2是c两个字节也就是cd。fos.write(b);// 关闭资源fos.close();} } 文件操作前cd 文件操作后cdabcde3. 输出数据 写出指定字节 public void write(int b) public void write(byte[] b) public void write(byte[] b,int off,int len) //从off索引开始len个字节 写出字节write(int b) 方法每次可以写出一个字节数据代码如下 public class IoWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos new FileOutputStream(fos.txt); // 写出数据fos.write(97); // 写出第1个字节fos.write(98); // 写出第2个字节fos.write(99); // 写出第3个字节// 关闭资源fos.close();} } 输出结果 abc写出数组 public class FOSWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos new FileOutputStream(fos.txt); // 字符串转换为字节数组byte[] b 麻麻我想吃烤山药.getBytes();// 写出字节数组数据fos.write(b);// 关闭资源fos.close();} } 输出结果 麻麻我想吃烤山药写出数组指定部分 public class FOSWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos new FileOutputStream(fos.txt); // 字符串转换为字节数组byte[] b abcde.getBytes();// 写出从索引2开始2个字节。索引2是c两个字节也就是cd。fos.write(b,2,2);// 关闭资源fos.close();} } 输出结果 cdInputStream 1. 构造方法 public FileInputStream(File file)以File对象为参数创建对象public FileInputStream(String name)根据文件名称创建对象 // 使用File对象创建流对象 File file new File(G:\\自动创建的文件夹\\a.txt); FileInputStream fos new FileInputStream(file);// 使用文件名称创建流对象 FileInputStream fos new FileInputStream(G:\\b.txt);2. 读取字节数据 read方法每次可以读取一个字节的数据提升为int类型读取到文件末尾返回-1。 代码测试如下【read.txt文件中内容为abcde】 public class FISRead {public static void main(String[] args) throws IOException{// 使用文件名称创建流对象FileInputStream fis new FileInputStream(read.txt);//read.txt文件中内容为abcde// 读取数据返回一个字节int read fis.read();System.out.println((char) read);read fis.read();System.out.println((char) read);read fis.read();System.out.println((char) read);read fis.read();System.out.println((char) read);read fis.read();System.out.println((char) read);// 读取到末尾,返回-1read fis.read();System.out.println( read);// 关闭资源fis.close();} } 输出结果 a b c d e -1循环读取 public class FISRead {public static void main(String[] args) throws IOException{// 使用文件名称创建流对象FileInputStream fis new FileInputStream(read.txt);// 定义变量保存数据int b // 循环读取while ((b fis.read())!-1) {System.out.println((char)b);}// 关闭资源fis.close();} } 输出结果 a b c d e3. 错误读取 read(byte[] b)每次读取b的长度个字节到数组中返回读取到的有效字节个数读取到末尾时返回-1 public class FISRead {public static void main(String[] args) throws IOException{// 使用文件名称创建流对象.FileInputStream fis new FileInputStream(read.txt); // read.txt文件中内容为abcde// 定义变量作为有效个数int len // 定义字节数组作为装字节数据的容器 byte[] b new byte[2];// 循环读取while (( len fis.read(b))!-1) {// 每次读取后,把数组变成字符串打印System.out.println(new String(b));}// 关闭资源fis.close();} }输出结果 ab cd ed由于read.txt文件中内容为abcde而错误数据d是由于最后一次读取时只读取一个字节e数组中上次读取的数据没有被完全替换【注意是替换看下图】所以要通过len 获取有效的字节 public class FISRead {public static void main(String[] args) throws IOException{// 使用文件名称创建流对象.FileInputStream fis new FileInputStream(read.txt); // 文件中为abcde// 定义变量作为有效个数int len // 定义字节数组作为装字节数据的容器 byte[] b new byte[2];// 循环读取while (( len fis.read(b))!-1) {// 每次读取后,把数组的有效字节部分变成字符串打印System.out.println(new String(b0len));// len 每次读取的有效字节个数}// 关闭资源fis.close();} }输出结果 ab cd eInputStream与OutputStream的实例 public class Copy {public static void main(String[] args) throws IOException {// 1.创建流对象// 1.1 指定数据源FileInputStream fis new FileInputStream(D:\\test.jpg);// 1.2 指定目的地FileOutputStream fos new FileOutputStream(test_copy.jpg);// 2.读写数据// 2.1 定义数组byte[] b new byte[1024];// 2.2 定义长度int len;// 2.3 循环读取while ((len fis.read(b))!-1) {// 2.4 写出数据fos.write(b, 0 , len);}// 3.关闭资源fos.close();fis.close();} } Reader 1. 构造方法 public FileReader(File file)以File对象为参数创建对象public FileReader(String name)根据文件名称创建对象 // 使用File对象创建流对象 File file new File(G:\\自动创建的文件夹\\a.txt); FileReader fr new FileReader(file);// 使用文件名称创建流对象 FileInputStream fos new FileInputStream(G:\\b.txt);2. 读取字符 read方法每次可以读取一个字符的数据提升为int类型读取到文件末尾返回-1 public class FRRead {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileReader fr new FileReader(a.txt);// 定义变量保存数据int b // 循环读取while ((b fr.read())!-1) {System.out.println((char)b);}// 关闭资源fr.close();} }Writer 1. 构造方法 public FileWriter(File file)以File对象为参数创建对象public FileWriter(String name)根据文件名称创建对象 public class FileWriterConstructor {public static void main(String[] args) throws IOException {// 第一种使用File对象创建流对象File file new File(a.txt);FileWriter fw new FileWriter(file);// 第二种使用文件名称创建流对象FileWriter fw new FileWriter(b.txt);} }2. 续写和换行 public class FWWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象可以续写数据FileWriter fw new FileWriter(fw.txttrue); // 写出字符串fw.write(哥敢);// 写出换行fw.write(\r\n);// 写出字符串fw.write(摸屎);// 关闭资源fw.close();} } 输出结果: 哥敢 摸屎3. 写出数据 public class FWWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileWriter fw new FileWriter(fw.txt); // 写出数据fw.write(97); // 写出第1个字符fw.write(b); // 写出第2个字符fw.write(C); // 写出第3个字符//关闭资源时,与FileOutputStream不同。 如果不关闭,数据只是保存到缓冲区并未保存到文件。// fw.close();} } 输出结果 abC4. 关闭close与刷新flush flush 刷新缓冲区流对象可以继续使用。close:先刷新缓冲区然后通知系统释放资源。流对象不可以再被使用了。 public class FWWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileWriter fw new FileWriter(fw.txt);// 写出数据通过flushfw.write(刷); // 写出第1个字符fw.flush();fw.write(新); // 继续写出第2个字符写出成功fw.flush();// 写出数据通过closefw.write(关); // 写出第1个字符fw.close();fw.write(闭); // 继续写出第2个字符,【报错】java.io.IOException: Stream closedfw.close();} }FileReader和FileWriter的实例 import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;public class CopyFile {public static void main(String[] args) throws IOException {//创建输入流对象FileReader frnew FileReader(F:\\新建文件夹\\aa.txt);//文件不存在会抛出java.io.FileNotFoundException//创建输出流对象FileWriter fwnew FileWriter(C:\\copyaa.txt);/*创建输出流做的工作* 1、调用系统资源创建了一个文件* 2、创建输出流对象* 3、把输出流对象指向文件 * *///文本文件复制一次读一个字符copyMethod1(fr, fw);//文本文件复制一次读一个字符数组copyMethod2(fr, fw);fr.close();fw.close();}public static void copyMethod1(FileReader fr, FileWriter fw) throws IOException {int ch;while((chfr.read())!-1) {//读数据fw.write(ch);//写数据}fw.flush();}public static void copyMethod2(FileReader fr, FileWriter fw) throws IOException {char chs[]new char[1024];int len0;while((lenfr.read(chs))!-1) {//读数据fw.write(chs,0,len);//写数据}fw.flush();} }CopyFile缓冲流 1. 缓冲流的基本原理 使用了底层流对象从具体设备上获取数据并将数据存储到缓冲区的数组内。 通过缓冲区的read()方法从缓冲区获取具体的字符数据这样就提高了效率。 如果用read方法读取字符数据并存储到另一个容器中直到读取到了换行符时将另一个容器临时存储的数据转成字符串返回就形成了readLine()功能。 也就是说在创建流对象时会创建一个内置的默认大小的缓冲区数组通过缓冲区读写减少系统IO次数从而提高读写的效率。 2. 缓冲流的分类 字节缓冲流BufferedInputStreamBufferedOutputStream字符缓冲流BufferedReaderBufferedWriter 3. 示例 //1. 创建字节缓冲输入流 BufferedInputStream bis new BufferedInputStream(new FileInputStream(b.txt));//2. 创建字节缓冲输出流 BufferedOutputStream bos new BufferedOutputStream(new FileOutputStream(b.txt));//3. 创建字符缓冲输入流 BufferedReader br new BufferedReader(new FileReader(b.txt));//4. 创建字符缓冲输出流 BufferedWriter bw new BufferedWriter(new FileWriter(b.txt));转换流 1. InputStreamReader类-----(字节流到字符流的桥梁) 转换流java.io.InputStreamReader是Reader的子类从字面意思可以看出它是从字节流到字符流的桥梁。它读取字节并使用指定的字符集将其解码为字符。 构造方法 InputStreamReader(InputStream in): 创建一个使用默认字符集的字符流。InputStreamReader(InputStream in, String charsetName): 创建一个指定字符集的字符流。 InputStreamReader isr new InputStreamReader(new FileInputStream(in.txt)); InputStreamReader isr2 new InputStreamReader(new FileInputStream(in.txt) , GBK);2. OutputStreamWriter类-----(字符流到字节流的桥梁) 字面看容易混淆会误以为是转为字符流其实不然OutputStreamWriter为从字符流到字节流的桥梁。使用指定的字符集将字符编码为字节。它的字符集可以由名称指定也可以接受平台的默认字符集。 序列化与ObjectInputStream、ObjectOutputStream 1. 何谓序列化 Java 提供了一种对象序列化的机制。用一个字节序列可以表示一个对象该字节序列包含该对象的数据、对象的类型和对象中存储的属性等信息。字节序列写出到文件之后相当于文件中持久保存了一个对象的信息。 反之该字节序列还可以从文件中读取回来重构对象对它进行反序列化。对象的数据、对象的类型和对象中存储的数据信息都可以用来在内存中创建对象。看图理解序列化 2. 序列化和反序列化的两个类 ObjectOutputStream 类将Java对象的原始数据类型写出到文件,实现对象的持久存储。ObjectInputStream类将之前使用ObjectOutputStream序列化的原始数据恢复为对象。 创建可序列化对象必须满足两个条件 该类必须实现java.io.Serializable 接口Serializable 是一个标记接口不实现此接口的类将不会使任何状态序列化或反序列化会抛出NotSerializableException 。该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的则该属性必须注明是瞬态的使用transient 关键字修饰。 public class Employee implements java.io.Serializable {public String name;public String address;public transient int age; // transient瞬态修饰成员,不会被序列化public void addressCheck() {System.out.println(Address check : name -- address);} }序列化对象 public final void writeObject (Object obj) : 将指定的对象写出。public class SerializeDemo{public static void main(String [] args) {Employee e new Employee();e.name zhangsan;e.address beiqinglu;e.age 20; try {// 创建序列化流对象ObjectOutputStream out new ObjectOutputStream(new FileOutputStream(employee.txt));// 写出对象out.writeObject(e);// 释放资源out.close();fileOut.close();System.out.println(Serialized data is saved); // 姓名地址被序列化年龄没有被序列化。} catch(IOException i) {i.printStackTrace();}} } 输出结果 Serialized data is saved反序列化创建对象 public final Object readObject () : 读取一个对象。 public class DeserializeDemo {public static void main(String [] args) {Employee e null;try { // 创建反序列化流FileInputStream fileIn new FileInputStream(employee.txt);ObjectInputStream in new ObjectInputStream(fileIn);// 读取一个对象e (Employee) in.readObject();// 释放资源in.close();fileIn.close();}catch(IOException i) {// 捕获其他异常i.printStackTrace();return;}catch(ClassNotFoundException c) {// 捕获类找不到异常System.out.println(Employee class not found);c.printStackTrace();return;}// 无异常,直接打印输出System.out.println(Name: e.name); // zhangsanSystem.out.println(Address: e.address); // beiqingluSystem.out.println(age: e.age); // 0} }打印流 1. 分类 字节打印流PrintStream字符打印流PrintWriter 2. 示例 PrintStream复制文本文件 public class PrintStreamDemo {public static void main(String[] args) throws IOException {BufferedReader brnew BufferedReader(new FileReader(copy.txt));PrintStream psnew PrintStream(printcopy.txt);String line;while((linebr.readLine())!null) {ps.println(line);}br.close();ps.close();} }PrintWriter复制文本文件 import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; /*** 使用打印流复制文本文件*/ public class PrintWriterDemo {public static void main(String[] args) throws IOException {BufferedReader brnew BufferedReader(new FileReader(aa.txt));PrintWriter pwnew PrintWriter(printcopyaa.txt);String line;while((linebr.readLine())!null) {pw.println(line);}br.close();pw.close();} }Properties 我想各位对这个Properties类多多少少也接触过了首先Properties类并不在IO包下那为啥要和IO流一起讲呢原因很简单因为properties类经常和io流的联合一起使用。 1. 特有功能 public Object setProperty(String key,String value)public String getProperty(String key)public Set stringPropertyNames() public class ProDemo {public static void main(String[] args) throws FileNotFoundException {// 创建属性集对象Properties properties new Properties();// 添加键值对元素properties.setProperty(filename, a.txt);properties.setProperty(length, 209385038);properties.setProperty(location, D:\\a.txt);// 打印属性集对象System.out.println(properties);// 通过键,获取属性值System.out.println(properties.getProperty(filename));System.out.println(properties.getProperty(length));System.out.println(properties.getProperty(location));// 遍历属性集,获取所有键的集合SetString strings properties.stringPropertyNames();// 打印键值对for (String key : strings ) {System.out.println(key -- properties.getProperty(key));}} } 输出结果 {filenamea.txt, length209385038, locationD:\a.txt} a.txt 209385038 D:\a.txt filename -- a.txt length -- 209385038 location -- D:\a.txt 2. IO流结合 把键值对形式的文本文件内容加载到集合中 public void load(Reader reader) public void load(InputStream inStream) 3. 把集合中的数据存储到文本文件中 public void store(Writer writer,String comments) public void store(OutputStream out,String comments) public class ProDemo {public static void main(String[] args) throws FileNotFoundException {// 创建属性集对象Properties pro new Properties();// 加载文本中信息到属性集pro.load(new FileInputStream(Properties.txt));// 遍历集合并打印SetString strings pro.stringPropertyNames();for (String key : strings ) {System.out.println(key -- pro.getProperty(key));}} } 输出结果 filename -- Properties.txt length -- 123 location -- C:\Properties.txt
http://www.dnsts.com.cn/news/19742.html

相关文章:

  • 庆阳网站设计厂家品牌推广活动
  • 微网站建设86215网站关键词优化到首页难度
  • 哪有做奇石网站wordpress标签页固定连接以.html后缀结尾
  • 厦门网站设计制作校园网站建设素材
  • 公司介绍网站怎么做的我有域名和云服务器怎么做网站
  • 做网站一般使用什么算法专线网站建设
  • 如何将vs做的网站备份出来三站合一网站建设方案
  • pcms网站开发学ui去哪个机构比较好
  • 项目经理网站开发流程wordpress导航菜单添加图标
  • 大连做网站一般给多大空间国外做贸易网站
  • 网站都有哪些国内最新新闻热点事件摘抄
  • 公司建个网站要多少钱电子购物网站设计
  • 网站还没建设好可以备案吗网站开发的前端语言是哪些
  • 可以写代码的网站上海企业推广
  • 上海高端品牌网站建设杭州有哪些性价比高的网站建设服务商
  • 响应式网站居中搭建的网站403
  • 网站如何注册域名怎样说服老板做网站
  • wordpress网站主题物理网络设计是什么
  • 做网站以后的趋势深圳注册公司股东
  • 服务器买好了怎么搭建自己的网站网站设计 线框图
  • 自己制作动漫的软件网站seo解决方案
  • 手机网站优化排名首页顺义建站设计
  • 企业网站建设开题报告罗田县住房和城乡建设局网站
  • 手机网站开发解决方案做教育类网站
  • wix怎么做网站教程自动编程软件
  • 建设企业网站找谁哪些行业做网站推广的多
  • 无锡网络公司网站建设app微信公众号平免费素材网站图库
  • 个人博客网站搭建网站营销外包公司简介
  • 大连做环评网站站长工具推荐
  • 吴江网站建设哪家好昌江区网站建设