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

wordpress 小说站莱州网站建设

wordpress 小说站,莱州网站建设,wordpress 显示指定文章,青岛网站优化公司哪家好IO流 IO#xff1a;Input / Output 完成输入 / 输出 应用程序运行时——数据在内存中 ←→ 把数据写入硬盘#xff08;磁带#xff09; 内存中的数据不可持久保存 输入#xff1a;从外部存储器#xff08;硬盘、磁带、U盘#…IO流 IOInput / Output 完成输入 / 输出 应用程序运行时——数据在内存中                        ←→                把数据写入硬盘磁带 内存中的数据不可持久保存 输入从外部存储器硬盘、磁带、U盘把数据读入内存中。 输出从内存中把数据写入外部存储器硬盘、磁带、U盘中这样就可以保证即使程序退出了数据依然不会丢失。 File —— 代表磁盘上的文件或目录 listRoot列出磁盘上所有的根目录 exists判断是否存在 mkdir创建目录 listFiles列出当前目录下所有的文件和子目录 listFilesFileFilter filter列出当前目录下符合指定条件的文件和子目录 listFilesFilenameFilter filter 列出某个磁盘所有的文件——递归 import java.io.File;public class ListE {public static void main(String[] args) {// 创建一个File它代表了E盘File e new File(e:/Program Files);List(e);}public static void List(File dir) {System.out.println(dir 目录下包含的文件和子目录有);// 该方法返回当前目录所包含的所有文件和子目录File[] files dir.listFiles();for (File file : files) {System.out.println( file);// 如果file是目录继续列出该目录下所有文件if (file.isDirectory()) {List(file);}}} } 列出某个磁盘所有的.txt文件——递归 import java.io.*;public class FileFilterTest {public static void main(String[] args) {File e new File(e:/);// 返回当前目录所包含的所有*.txt文件此时就需要对文件进行过滤File[] files e.listFiles(new FileFilter() {// pathname就代表正在处理的文件如果该方法返回true意味着该文件就被保留否则该方法将会被过滤掉Override// 没有比IOExcepiton更小的所以只能用try catchpublic boolean accept(File pathname) {// 说明文件名以.txt结尾try {if (pathname.getCanonicalPath().endsWith(txt)) {return true;}} catch (IOException e) {e.printStackTrace();}return false;}});for (File file : files) {System.out.println(file);}} } File的特征只能访问磁盘上的文件和目录它无法访问文件内容。 如果要访问文件的内容必须使用IO流。 流的分类 1、按流的方向来分从程序所在内存的角度来看 1输入流 把外部输入读入当前程序所在内存 2输出流 把当前程序所在内存的数据输出到外部 2、按流处理的数据来分 1字节流 处理数据单位是字节8bit适应性广、功能强大 2字符流 处理的数据单元是字符。通常来说它主要用于处理文本文件 在处理文本文件时比字节流方便 3、按流的角色来分 1节点流 直接和一个IO的物理节点磁盘上的文件、网络关联 2包装流处理流 / 过滤流 以节点为基础包装之后得到的流 都继承了FilterXxx等基类 流的概念模型 输入流中的数据单元放入应用程序应用程序中的数据单元放入输出流。 缓冲流 外部存储器的速度比内存的速度慢外部存储器的读写与内存的读写并不同步 ——通过缓冲就可以解决这种不同步 反正你把流用完了 - 别忘记调用flush方法把缓冲中的内容刷入实际的节点 调用close()也可——系统会在关闭之前自动刷缓冲 IO流一共涉及40多个类 字节流字符流节点流InputStreamOutputStreamReaderWriter抽象FileInputStreamFileOutputStreamFileReaderFileWriter访问文件ByteArrayInputStreamByteArrayOutputStreamCharArrayReaderCharArrayWriter数组PipedInputStreamPipedOutputStreamPipedReaderPipedWriter访问管道StringReaderStringWriter访问字符串缓冲流BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter缓冲FilterInputStreamFilterOutputStreamFilterReaderFilterWriter抽象PrintStreamPrintWriter打印InputStreamReaderOutputStreamWriter转换DataInputStreamDataOutputStream特殊 所有以InputStream结尾的都是字节输入流 所有以OutputStream结尾的都是字节输出流 所有以Reader结尾的都是字符输入流 所有以Writer结尾的都是字符输出流 1、FileInputStream  import java.io.*; public class FileInputStreamTest {public static void main(String[] args) throws IOException {// 创建输入流相当于得到一根有水的水管FileInputStream fis new FileInputStream(D:\\Program Files\\eclipse-java-2023-06-R-win32-x86_64\\workplace\\day\\src\\day13\\FileInputStreamTest.java);// System.out.print((char)fis.read());// 该方法每次只读一个字节/** 为了把fis中水滴全部“取出”内存有两个做法* 1、用大桶* 2、用循环*/byte[] buff new byte[64]; // 我的水桶可以装64个“水滴”int hasRead -1;//hasRead fis.read(buff);// 用“桶”从fis水管中取水while((hasRead fis.read(buff)) 0) {// 上一次读取了几个字节此处就输出几个字节System.out.println(new String(buff, 0 ,hasRead));}} } 运行结果  2、FileOutputStream import java.io.FileOutputStream; import java.io.IOException;public class FileOutputStreamTest {public static void main(String[] args) {FileOutputStream fos null;try {// 得到输出流相当于得到一个没有水滴的水管fos new FileOutputStream(abc.txt);fos.write(97); // 每次输出一个字节“fos.write(98);fos.write(99);fos.write(100);fos.write(101);fos.write(自由、民主的普世价值观.getBytes());} catch (IOException ex) {ex.printStackTrace();} finally {try {fos.close();} catch (Exception ex) {ex.printStackTrace();}} 运行结果  复制文本文件 import java.io.*; public class CopyTest {public static void main(String[] args) {// JDK 7提供了自动关闭资源的try语句try (// 创建输入流得到一个有水滴的水管FileInputStream fis new FileInputStream(D:\\Program Files\\eclipse-java-2023-06-R-win32-x86_64\\workplace\\day\\src\\day13\\CopyTest.java);// 创建输出流得到一个空水管FileOutputStream fos new FileOutputStream(D:\\Program Files\\eclipse-java-2023-06-R-win32-x86_64\\workplace\\day\\src\\day13\\test.txt); ){int hasRead -1;byte[] buff new byte[128];// 从fis里读取水滴放入buff中while((hasRead fis.read(buff)) 0) {// 将buff中水滴写入foshasRead用于控制读了多少就写多少fos.write(buff, 0, hasRead);}} catch (IOException ex) {ex.printStackTrace();}} } 运行结果  节点流直接与IO节点关联 ——IO节点有很多键盘、网络、文件、磁带…… 过滤流建立在节点流的基础之上 过滤流的好处 ——消除底层节点之间的差异 ——使用过滤流的方法执行IO更加便捷 FileOutputStream →PrintStream FileWriter→PrintWriter 如果已经知道要读的内容是字符内容就可按如下方式转换 InputStream→InputStreamReader→BufferedReader 1、 FileOutputStream →PrintStream import java.io.*;public class PrintStreamTest {public static void main(String[] args) {try (// 创建节点流节点流使用不方便FileOutputStream fos new FileOutputStream(D:\\Program Files\\eclipse-java-2023-06-R-win32-x86_64\\workplace\\day\\src\\day13\\my.txt); // 把节点流包装成过滤流消除节点流的差异而且PrintStream的方法更加方便PrintStream ps new PrintStream(fos); ){ps.println(我想);ps.println(我想早);ps.println(我想早点);} catch (Exception ex) {ex.printStackTrace();} } } 运行结果  3、DataInputStream与DataOutputStream 它们是两个特殊的流——它们是过滤流建立在已有IO的基础之上 ——它们只要增加一些特定的方法来读取特定的数据。 import java.io.*; public class DataOutputStreamTest {public static void main(String[] args) {try (// 创建节点流——与磁盘上的文件关联FileOutputStream fos new FileOutputStream(price.txt);// 创建过滤流过滤流建立在节点流的基础上DataOutputStream dos new DataOutputStream(fos);){dos.writeDouble(3.4);dos.writeDouble(5.23);dos.writeDouble(4.34);dos.writeDouble(5.12);} catch (Exception ex) {ex.printStackTrace();} } } 运行结果 import java.io.*; public class DataInputStreamTest {public static void main(String[] args) {try (// 先创建节点流与指定物理节点文件建立读写FileInputStream fis new FileInputStream(price.txt);// 以节点流来创建过滤流DataInputStream dis new DataInputStream(fis);){System.out.println(dis.readDouble());System.out.println(dis.readDouble());System.out.println(dis.readDouble());} catch (Exception ex) {ex.printStackTrace();}} } 运行结果 重定向标准输入输出 System.in——标准输入。通常代表键盘。 System.out——标准输出。通常代表屏幕。 System.setOut() ——可以将标准输出重定向另一个输出流。 import java.io.*; public class SetOutTest {public static void main(String[] args) throws Exception{// 可以将标准输出重定向到指定的输出流System.setOut(new PrintStream(out.txt));System.out.println(ABC);System.out.println(ABC);System.out.println(ABC);System.out.println(ABC);} } 运行结果输出的内容重定向到out.txt文本文件中  System.setIn() ——可以将标准输出重定向另一个输入流。 import java.io.*; public class RedirectKeyIn {public static void main(String[] args) throws Exception {// 将标准输入重定向到RedirectKeyIn.javaSystem.setIn(new FileInputStream(RedirectKeyIn.java));//System.in它是一个节点流一般关联着物理键盘//直接用System.In(InputStream——节点、字节、输入流可以读取键盘输入//缺点是太繁琐、而且效率相当低下//System.out.println(System.in.read());InputStreamReader reader new InputStreamReader(System.in);BufferedReader br new BufferedReader(reader);String line null;// 控制BufferedReader每次读取一行while ((line br.readLine()) ! null) {System.out.println(line);}} } 运行结果 Java虚拟机读取其他进程的数据  Java如何启动其他进程Runtime实例.exec() 该方法的返回值是一个Process对象 Process——代表一个进程。 进程就是运行中的应用程序。 import java.io.*; public class ReadFromProcess {public static void main(String[] args) throws Exception{Runtime runtime Runtime.getRuntime();// 启动javac应用程序返回该应用程序对应的进程 // Process proc runtime.exec(javac.exe -encoding UTF8 -d . ReadFromProcess.java);Process proc runtime.exec(javac.exe -encoding UTF8 -d . ReadFromProcess);// 要得到javac应用程序输出的内容此处应该用输入流还是输出流// 对于javac来说是输出但对于我们应用程序来说用输入流InputStreamReader reader new InputStreamReader(proc.getErrorStream());BufferedReader br new BufferedReader(reader);String line null;StringBuilder sb new StringBuilder();// 控制BufferedReader每次读取一行while ((line br.readLine()) ! null) {//System.out.println(line);sb.append(line);}// 如果有错误输出if (sb.toString().length() 0) {System.out.println(编译出错错误信息如下----);// 输出错误提示System.out.println(sb);} else {System.out.println(成功完成----);}} } 上面为“编译出错”的代码下面为“成功完成”的代码  RandomAccessFile——随意任意访问文件 Random——想访问文件的哪个点就访问文件的哪个点任意 RandomAccessFile实现了Closeable接口所以可以使用自动关闭资源的try语句。 特征 1、既可读、又可写、还可追加。相当于InputStream与OutputStream合体。 RandomAccessFile它不会覆盖原有的文件内容。 2、只能访问文件这就是它的局限性。 import java.io.*; public class RandomAccessFileTest {public static void main(String[] args) {try (// 使用RadomAccessFile创建一个只读的输入流RandomAccessFile raf new RandomAccessFile(RandomAccessFileTest.java, r);){byte[] buff new byte[1024];int hasRead -1;while ((hasRead raf.read(buff)) 0) {System.out.println(new String(buff, 0, hasRead));}} catch (Exception ex) {ex.printStackTrace();}} } 创建RandomAccessFile需要指定读r、写rw模式 体现它的“random【任意】”性的方法 seek(long pos)——用于把记录指针移动到任意位置想访问哪个点就访问哪个点。 一开始它的记录指针位于文件的开始位置。 使用RandomAccessFile来追加文件内容 1、把记录指针移动到最后 2、执行输出即可 import java.io.RandomAccessFile;public class AppendTest {public static void main(String[] args) {try (// 使用RadomAccessFile创建一个只读的输入流RandomAccessFile raf new RandomAccessFile(AppendTest.java, rw);){// 把记录指针移动到文件的最后raf.seek(raf.length());raf.write(//做人好累.getBytes()); } catch (Exception ex) {ex.printStackTrace();}} } 使用RandomAccessFile来插入文件内容 1、把记录指针移动到指定位置 2、从当前位置到文件结尾的内容先读取并保存 3、输出要插入的内容 序列化 Java对象内存-二进制流 目的 1、在有些时候需要把对象存储到外部存储器中持久化保存。 2、在有些时候需要把对象通过网络传输。 可序列化的对象 Java要求序列化的类实现下面任意两个接口 1、Serializable接口只是一个标记性的接口实现该接口无需实现任何方法 2、Externalizable实现该接口要实现方法。 序列化的IO流 ObjectInputStream——负责从二进制流“恢复”对象。readObject ObjectOutputStream——负责把对象保存到二进制流中。writeObject import java.io.*; class Apple implements Serializable{private String name;private String color;private double weight;public Apple() {super();}public Apple(String name, String color, double weight) {super();this.name name;this.color color;this.weight weight;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getColor() {return color;}public void setColor(String color) {this.color color;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight weight;}public String toString() {return Apple[name name ,color color ,weight weight ];} }public class WriteObject {public static void main(String[] args) {Apple ap new Apple(红富士, 红色, 3.4);System.out.println(ap);// 当程序结束时虚拟机退出内存中的Apple对象就被销毁了try (ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(app.bin));) {oos.writeObject(ap); //把ap对象写入app.bin文件中}catch (Exception ex) {// TODO: handle exception}} } import java.io.*; public class ReadObject {public static void main(String[] args) {try (ObjectInputStream ois new ObjectInputStream(new FileInputStream(app.bin));){Apple ap (Apple)ois.readObject();System.out.println(ap);}catch (Exception ex) {ex.printStackTrace();}} } 序列化机制 总结 Java传统IO的基本体系 字节输入流字节输出流字符输入流字符输出流访问文件InputStreamOutputStreamReaderWriter访问数组FileXxx访问管道ByteArrayXxxCharArrayXxx访问字符串StringXxx过滤流FilterInputStreamFilterOutputStreamFilterReaderFilterWriter打印流PrintStreamPrintWriter转换流InputStreamReaderOutputStreamWriter特殊流DataInputStreamDataOutputStreamObjectInputStreamObjectOutputStream RandomAccessFile 1、它只能访问文件。相当于DataInputStream和DataOutputStream组合 2、任意由seek(int pos)。
http://www.pierceye.com/news/212034/

相关文章:

  • 贵金属网站模板阿里云建设网站的流程
  • 维护一个网站要多少钱个人网页设计的主要内容和要求
  • 西安网站优化推广方案新网站做百度百科
  • 网站外链建设到底该怎么做flash网站案例
  • 成都私人网站制作卓越网站建设的优点
  • 做网站下载别人的图算不算侵权源码之家免费
  • 宁夏住房城乡建设厅网站应用网站建设
  • 宾馆网站建设网站建设管理规范
  • 内部网站建设的步骤过程选择邯郸做网站
  • 国外免费外贸网站dw网页制作教程个人网站
  • 西安建设局网站地址室内设计效果图一套方案
  • php 建网站电子商务网站建设项目规划书
  • 常熟建设局网站代理办营业执照的公司
  • 济南网站关键词优化公司如何制作网站赚钱
  • 长春旅游网站开发360投放广告怎么收费
  • 微信公众号做网站卖东西静态化网站的缺点
  • 网站空间购买今天的新闻头条最新消息
  • 网站制作教程图解怎么解压wordpress
  • 唐山市城市建设规划局网站腾讯云建设一个网站要多少钱
  • 邢台集团网站建设费用聚牛建设网站
  • 如何创建电子商务网站学校网站设计首页
  • 扬州建设投资集团网站世界总人口实时数据
  • 沧州制作网站食品商务网-网站建设
  • 0592 网站建设模板网站建设+百度
  • 请人做个网站多少钱免费商城app
  • 网站建设包括哪些方面?手游源码网站
  • 机关门户网站建设管理情况软件开发工具都有哪些
  • 官方网站建设专家磐石网络wordpress对应的id
  • 学生自做网站优秀作品徐州企业建站模板
  • 网络电子商务购物网站idc机房建设