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

河南郑州网站设计公司哪个网站做高中的题好

河南郑州网站设计公司,哪个网站做高中的题好,网站是怎么挣钱的,35互联做的网站【今日】 孩儿立志出乡关#xff0c;学不成名誓不还。 文件输入/输出流 程序运行期间#xff0c;大部分数据都在内存中进行操作#xff0c;当程序结束或关闭时#xff0c;这些数据将消失。如果需要将数据永久保存#xff0c;可使用文件输入/输出流与指定的文件建立连接学不成名誓不还。 文件输入/输出流 程序运行期间大部分数据都在内存中进行操作当程序结束或关闭时这些数据将消失。如果需要将数据永久保存可使用文件输入/输出流与指定的文件建立连接将需要的数据永久保存到文件中。 一    FilelnputStream与FileOutputStream类 文件字节流 FileInputStream类与FileOutputStream类都用来操作磁盘文件。如果用户的文件读取需求比较简单则可以使用FileInputStream类该类继承自InputStream类。FileOutputStream类与 FilelnputStream类对应提供了基本的文件写入能力。FileOutputStream类是OutputStream类的子类。FileInputStream类常用的构造方法如下 ‍️FileInputStream(String name) ‍️FileInputStream(File file)  第一个构造方法使用给定的文件名name创建一个 FilelnputStream对象第二个构造方法使用File对象创建 FileInputStream对象。第一个构造方法比较简单但第二个构造方法允许在把文件连接输入流之前对文件做进一步分析。 FileOutputStream类有与FileInputStream类相同的参数构造方法创建一个FileOutputStream对象时可以指定不存在的文件名但此文件不能是一个已被其他程序打开的文件。 【代码块】输出流 package mt;import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt); //在MyProject下创建文本word.txtFileOutputStream out null; //赋予空值try {out new FileOutputStream(f);String str 你见过凌晨4点的洛杉矶吗;byte b[] str.getBytes();//字符串转换为字节数组try {out.write(b);} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();}finally {if(out !null) {try {out.close();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}} } 还没运行前可以看到左侧的项目中并没有word.txt项目。  【运行刷新】 当我们反复向这个文件中写值的时候它会覆盖前面的内容。 如果我们将out new FileOutputStream(f); 改为out new FileOutputStream(f,true)文件输出流在文件末尾追加内容。 改为out new FileOutputStream(f,true)文件输出流替换内容。 【代码】输入流 package mt;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt); //在MyProject下创建文本word.txtFileOutputStream out null; //赋予空值try {out new FileOutputStream(f,false);String str 你见过凌晨4点的洛杉矶吗;byte b[] str.getBytes();//字符串转换为字节数组try {out.write(b);} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();}finally {if(out !null) {try {out.close();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}FileInputStream in null;try {in new FileInputStream(f);//输入流读取文件byte b2[] new byte[1024];//创建缓冲区in.read(b2);//将文件信息读入缓存数组中System.out.println(文本中的内容是new String(b2));} catch (IOException e) {e.printStackTrace();}finally {if(in!null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}} }【运行结果】 观察运行结果我们可以发现输出文本内容后后面还跟了一串空格这是因为我们创建的缓冲区字节数是1024远远大于这些汉字所占用的字节如何去除这些空格呢 我们可以这样做 这样做是因为in.read()可以返回所读取的数组的总长度在让它从索引0到len进行输出就可以 去除空格看一下运行结果 我们也可以直接对文本的内容进行修改只用输入流进行输出 我们先在word.txt里面写入歌词 【运行的代码】 package mt;import java.io.File; import java.io.FileInputStream; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt); //在MyProject下创建文本word.txtFileInputStream in null;try {in new FileInputStream(f);//输入流读取文件byte b2[] new byte[1024];//创建缓冲区int len in.read(b2);//读入缓冲区的总字节数System.out.println(文本中的内容是\nnew String(b2,0,len));} catch (IOException e) {e.printStackTrace();}finally {if(in!null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}} } 【运行结果】 二   FileReader和 FileWriter 类  文件字符流 使用FileOutputStream类向文件中写入数据与使用FileInputStream类从文件中将内容读出来都存在一点不足即这两个类都只提供了对字节或字节数组的读取方法。由于汉字在文件中占用两个字节如果使用字节流读取不好可能会出现乱码现象此时采用字符流FileReader类或 FileWriter类即可避免这种现象。      FileReader类和 FileWriter类对应了 FilelnputStream类和 FileOutputStream类。FileReader类顺序地读取文件只要不关闭流每次调用read(方法就顺序地读取源中其余的内容直到源的末尾或流被关闭。 【代码】输出流 package mt;import java.io.File; import java.io.FileWriter; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt);FileWriter fw null;try {fw new FileWriter(f);String str 只是一场烟火散落的尘埃;fw.write(str);} catch (IOException e) {e.printStackTrace();}finally {if(fw!null) {try {fw.close();} catch (IOException e) {e.printStackTrace();}}}} }【运行结果】  【代码】输入流 package mt;import java.io.File; import java.io.FileReader; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt);FileReader fr null;try {fr new FileReader(f);char ch[] new char[1024];int len fr.read(ch);System.out.println(文本内容为new String(ch,0,len));} catch (IOException e) {e.printStackTrace();}finally {if(fr!null) {try {fr.close();} catch (IOException e) {e.printStackTrace();}}}} } 【运行结果】  带缓存的输入/输出流 首先我们了解一下什么是缓冲区 我们要将一推箱子由A运到B地如果我们派人一次一次的去搬运是十分慢的。如果直接用货车运输那么方便许多。这里货车就充当了缓冲区的功能。 缓存是I/O的一种性能优化。缓存流为I1O流增加了内存缓存区使得在流上执行skip)、mark()和reset()方法都成为可能。 一   BufferedInputStream与 BufferedOutputStream类 缓冲字节流 BufferedInputStream 类可以对所有InputStream类进行带缓存区的包装以达到性能的优化。BufferedInputStream类有两个构造方法1.BufferedInputStream(InputStream in) 2.BufferedInputStream(InputStream in,int size 第一种形式的构造方法创建了一个有32个字节的缓存区。 第二种形式的构造方法按指定的大小来创建缓存区。 一个最优的缓存区的大小取决于它所在的操作系统、可用的内存空间以及机器配置。从构造方法可以看出BufferedInputStream对象位于InputStream类对象之后。 BufferedInputStream读取文件过程  使用 BufferedOutputStream类输出信息和仅用OutputStream类输出信息完全一样只不过BufferedOutputStream有一个flush)方法用来将缓存区的数据强制输出完。BufferedOutputStream类也有两个构造方法 BufferedOutputStream(OutputStream in)。 BufferedOutputStream(OutputStream in,int size)。 第一种构造方法创建一个有32个字节的缓存区。 第二种构造方法以指定的大小来创建缓存区。 缓冲输入流 不使用缓存区效果 【代码】 package mt;import java.io.File; import java.io.FileInputStream; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(D:\\FlashCenter\\歌词.txt);FileInputStream in null;long start System.currentTimeMillis();//获取流开始的毫秒值try {in new FileInputStream(f);byte b[] new byte[1024];//缓冲区字节数组这个缓冲区与Buffered不同while(in.read()!-1) {//当有值时循环输出}long end System.currentTimeMillis();//获取流结束的毫秒值System.out.println(运行经历的毫秒数:(end-start));} catch (IOException e) {e.printStackTrace();}finally {if(in !null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}} }【 运行效果】     使用缓冲流效果 【代码】 package mt;import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(D:\\FlashCenter\\歌词.txt);FileInputStream in null;BufferedInputStream bi null;long start System.currentTimeMillis();//获取流开始的毫秒值try {in new FileInputStream(f);bi new BufferedInputStream(in);//将文件字节流包装成缓冲字节流byte b[] new byte[1024];//缓冲区字节数组这个缓冲区与Buffered不同while(bi.read()!-1) {//当有值时循环输出}long end System.currentTimeMillis();//获取流结束的毫秒值System.out.println(运行经历的毫秒数:(end-start));} catch (IOException e) {e.printStackTrace();}finally {if(in !null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}if(bi!null) {try {bi.close();} catch (IOException e) {e.printStackTrace();}}}} } 【 运行效果】 从运行效果来看可以看出大大的提高了运行的效率。  缓冲输出流 【代码】 package mt;import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException;public class Demo2 {public static void main(String[] args) {File f new File(word.txt);FileOutputStream out null;BufferedOutputStream bo null;try {out new FileOutputStream(f);bo new BufferedOutputStream(out);//包装文件输出流String str 天生我才必有用千金散尽还复来;byte b[] str.getBytes();bo.write(b);//这里也能够提高效率//使用缓冲字节流输出时要多进行刷新操作。bo.flush();//刷新。强制将缓存区数据写入文件即使缓冲区没有写满。} catch (IOException e) {e.printStackTrace();}finally {try {out.close();} catch (IOException e) {e.printStackTrace();}}}} 【运行结果】 总结  无论BufferedInputStream与 BufferedOutputStream类在这里都有提高运行效率的结果。 二   BufferedReader与BufferedWriter类 缓冲字符流 BufferedReader类与BufferedWriter类分别继承Reader类与Writer类。这两个类同样具有内部事机制并能够以行为单位进行输入/输出。  BufferedReader类常用的方法如下 read0方法读取单个字符。 readLine()方法读取一个文本行并将其返回为字符串。若无数据可读则返回null。 BufferedWriter类中的方法都返回void。常用的方法如下 write(String s,int offint len)方法写入字符串的某一部分。 flush()方法刷新该流的缓存。 newLine(方法写入一个行分隔符。 在使用BufferedWriter类的Write()方法时数据并没有立刻被写入输出流而是首先进入缓存区中如果想立刻将缓存区中的数据写入输出流一定要调用flush)方法。 缓冲字符输出流代码实列  package mt;import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt);FileWriter fw null;BufferedWriter bw null;try {fw new FileWriter(f);bw new BufferedWriter(fw);//将文件字符输出流包装成缓存字符流String str1 世界那么大;String str2 我想去看看;bw.write(str1);//第一行bw.newLine();//创建新行bw.write(str2);//第二行} catch (IOException e) {e.printStackTrace();}finally {//要注意流关闭的顺序先创建的后关闭if(bw!null) {try {bw.close();} catch (IOException e) {e.printStackTrace();}}if(fw!null) {try {fw.close();} catch (IOException e) {e.printStackTrace();}}}} }运行效果  缓冲字符流输入代码实列  package mt;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt);FileReader fr null;BufferedReader br null;try {fr new FileReader(f);br new BufferedReader(fr);String tmp null;int i 1;while((tmp br.readLine())!null) {//循环读取文件中的内容System.out.println(第i行tmp);i;}br.readLine();//读一行} catch (IOException e) {e.printStackTrace();}finally {if(br!null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}}if(fr!null) {try {fr.close();} catch (IOException e) {e.printStackTrace();}}} } 运行效果  文本内容 输出效果 总结 BufferedReader与BufferedWriter类除了提高效率以外它还可以以行为单位来对字符数据进行操作。比如BufferedReader的readLine()方法BufferedWriter的newLine()方法。 数据的输入/输出流 数据输入/输出流(DataInputStream类与DataOutputStream类)允许应用程序以与机器无关的方式从底层输入流中读取基本Java数据类型。也就是说当读取一个数据时不必再关心这个数值应当是哪种字节。 一   DataInputStream类与DataOutputStream类 DatalnputStream类与DataOutputStream类的构造方法如下。 DataInputStream(InputStream in)使用指定的基础InputStream对象创建一个 DataInputStream对象。 DataOutputStream(OutputStream out)创建一个新的数据输出流将数据写入指定基础输出流。 DataOutputStream类提供了将字符串、double数据、int数据、boolean数据写入文件的方法。其中将字符串写入文件的方法有3种分别是writeBytes(String s)、writeChars(String s)、writeUTF(Strings)。由于Java中的字符是Unicode编码是双字节的writeBytes0方法只是将字符串中的每一个字符的低字节内容写入目标设备中而writeCharsO方法将字符串中的每一个字符的两个字节的内容都写到目标设备中writeUTFO方法将字符串按照UTF编码后的字节长度写入目标设备然后才是每一个字节的UTF编码。 DataInputStream类只提供了一个readUTF0方法返回字符串。这是因为要在一个连续的字节流读取一个字符串如果没有特殊的标记作为一个字符串的结尾并且不知道这个字符串的长度就无法知道读取到什么位置才是这个字符串的结束。DataOutputStream类中只有writeUTFO方法向目标设备中写入字符串的长度所以也能准确地读回写入字符串。 API中的部分方法 输入流 输出流  代码实列 package mt;import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt);FileOutputStream out null;DataOutputStream dos null;try {out new FileOutputStream(f);dos new DataOutputStream(out);//将文件流包装为数据流dos.writeUTF(这是写入字符串数据。); //写入字符串数据dos.writeDouble(3.14); //写入浮点型数据dos.writeBoolean(false); //写入布尔类型dos.writeInt(123); //写入整型数据} catch (IOException e) {e.printStackTrace();}finally {if(out!null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}if(dos!null) {try {dos.close();} catch (IOException e) {e.printStackTrace();}}}} } 我们运行以后发现运行结果为乱码 这是因为通过数据输出流写入文本的是字节码我们想要得到里面的数据就需要用数据输入流将里面的数据读出来。然后通过对应的方法进行解析得到结果。  我们添加上数据的输入流在来进行 代码如下 package mt;import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class Demo {public static void main(String[] args) {File f new File(word.txt);FileOutputStream out null;DataOutputStream dos null;try {out new FileOutputStream(f);dos new DataOutputStream(out);//将文件流包装为数据流dos.writeUTF(这是写入字符串数据。); //写入字符串数据dos.writeDouble(3.14); //写入浮点型数据dos.writeBoolean(false); //写入布尔类型dos.writeInt(123); //写入整型数据} catch (IOException e) {e.printStackTrace();}finally {if(out!null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}if(dos!null) {try {dos.close();} catch (IOException e) {e.printStackTrace();}}}DataInputStream di null;FileInputStream in null;try {in new FileInputStream(f);di new DataInputStream(in);System.out.println(readUTF()读取数据di.readUTF());System.out.println(readdouble()读取数据di.readDouble());System.out.println(readBoolean()读取数据di.readBoolean());System.out.println(readInt()读取数据di.readInt());} catch (IOException e) {e.printStackTrace();}finally {if(in!null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}if(di!null) {try {di.close();} catch (IOException e) {e.printStackTrace();}}}} } 运行结果 Thank!
http://www.hkea.cn/news/14473000/

相关文章:

  • 网站建设方为客户提供使用说明书学做网站论坛vip账户
  • 江苏有哪些做网站建设的公司wordpress表单文件上传
  • .net网站做优化广州深圳
  • 财经资讯网站该怎么做推广网站结构分类
  • 做网站要的软件商务网站建设策划书的格式
  • 越秀五屏网站建设通州区网站快速排名方案
  • 企业网站如何优化排名网站优化 价格查询
  • 建立个人网站的步骤有哪些企业网络采购平台
  • 网站品牌词优化怎么做Python能开发WordPress
  • 建设一个购物网站需要什么手机建筑设计app
  • 饥荒网站这么做福州seo建站
  • 长沙建长沙建网站公司网页模板psd
  • 东莞网站建设的方案常用的北京文化馆设计公司
  • 宜都市网站建设上海网站推广有哪些
  • 高端网站建设公司联系电话潍坊做网页的公司
  • 中国网站建设集团模仿大型门户网站做ppt
  • 有没有淄博张店做兼职工作的网站北京免费建站网络营销
  • 网站治做啊app简网站开发平台
  • 泉州网站建设方案详细合肥网站优化哪家好
  • 在北京网站建设的岗位网站文件服务器
  • 八戒网站建设wordpress 自定义 分类
  • 网站建设用款怎样使用仿站小工具做网站
  • 网站怎么做维护做庭院的网站
  • 购物网站图片的放大怎么做的公司网页设计内容方案
  • 做泵阀生意到哪个网站网页设计与制作精品课程网站
  • 页游网站网站标题怎么写
  • 智联招聘网站怎么做微招聘信息吗江苏省建设厅网站 投诉
  • 成都网站建设优创智汇怎么建立挣流量的网站
  • 网站开发路径河南建设工程信息网下载
  • 免费企业建站开源系统杭州外贸网站建设公司价格