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

辛集市网站建设_网站建设公司_ASP.NET_seo优化

一个做网站的软件,廊坊网站排名优化公司哪家好,中企动力全球邮企业邮箱,网站关键词不稳定迁移学习 简而言之这篇文章试图涵盖java.io中的一整套操作。 与与此主题相关的其他书籍和博客相比#xff0c;我的动机是通过案例研究来展示“操作方法”。 曾经是Java的学生#xff0c;我意识到学习一种新的程序语言的最有效方法是通过示例#xff1a;复制并粘贴一段代码我的动机是通过案例研究来展示“操作方法”。 曾经是Java的学生我意识到学习一种新的程序语言的最有效方法是通过示例复制并粘贴一段代码运行它以查看结果然后尝试逐步修改并重新运行它。 。 因此我认为这篇文章会有所帮助。 值得注意的是本文不会涉及任何与java.nio相关的内容因为我认为这是一个完全不同的主题。 目录 情况0创建一个新文件 情况1File中的两个常量 情况2删除文件 情况3创建目录 情况4列出给定目录中的文件和目录 情况5测试文件是否为文件 情况6写入RandomAccessFile 情况7将字节写入文件 情况8将字节追加到文件 情况9从文件读取字节 情况10复制文件 情况11将字符写入文件 情况12从文件中读取字符 情况13从OutputStream转换为FileWriter 情况14从InputStream转换为FileReader 案例15使用管道 情况16将格式化的字符串写入文件 案例17重定向“标准” IO 情况18逐行读取文件 案例19压缩到一个zip文件 案例20从zip文件中提取 情况21推回字节 情况0创建一个新文件 import java.io.File; public class FileOperationTest {public static void main(String[] args) {File f new File(helloworld.txt);try {f.createNewFile();} catch (Exception e) {e.printStackTrace();}} } 输出 如果之前没有“ helloword.txt”则在工作目录中创建一个新的空文件。 情况1File中的两个常量 import java.io.File; public class FileOperationTest {public static void main(String[] args) {System.out.println(File.separator);System.out.println(File.pathSeparator);} } 输出 / : 我得到上面的输出因为我正在Linux上工作。 如果使用Windows则输出应为\和; 。 可以看出出于可移植性和鲁棒性的目的应始终建议使用这两个常量。 情况2删除文件 import java.io.File; public class FileOperationTest {public static void main(String[] args) {File f new File(helloworld.txt);if (f.exists()) {if (!f.delete()) {System.out.println(the file cannot be deleted.);}} else {System.out.println(the file does not exist.);}} }情况3创建目录 import java.io.File; public class FileOperationTest {public static void main(String[] args) {File f new File(hello);f.mkdir();} }情况4列出给定目录中的文件和目录 import java.io.File; public class FileOperationTest {public static void main(String[] args) {File f new File(.);for (String str : f.list()) {System.out.println(str);}} } 输出我正在使用Eclipse .settings .classpath .project src bin 文件 。 list返回一个字符串数组。 如果您更喜欢File的数组请使用File 。 listFiles import java.io.File; public class FileOperationTest {public static void main(String[] args) {File f new File(.);for (File subFile : f.listFiles()) {System.out.println(subFile.getName());}} }情况5测试文件是否为文件 import java.io.File; public class FileOperationTest {public static void main(String[] args) {File f new File(helloworld.txt);if (f.isFile()) {System.out.println(YES);} else {System.out.println(NO);}} } 与File结合。 listFiles 我们可以列出给定目录及其子目录中的所有文件。 import java.io.File;public class FileOperationTest {public static void main(String[] args) {File f new File(.);listFiles(f);}private static void listFiles(File f) {if (f.isFile()) {System.out.println(f.getName());return;}for (File subFile : f.listFiles()) {listFiles(subFile);}} } 输出与案例4进行比较以查看差异 org.eclipse.jdt.core.prefs .classpath .project FileOperationTest.java FileOperationTest.class情况6写入RandomAccessFile import java.io.IOException; import java.io.RandomAccessFile;public class FileOperationTest {public static void main(String[] args)throws IOException {RandomAccessFile file new RandomAccessFile(helloworld.txt, rw);file.writeBytes(hello world!);file.writeChar(A);file.writeInt(1);file.writeBoolean(true);file.writeFloat(1.0f);file.writeDouble(1.0);file.close();} } 如果使用文本编辑器打开文件则会发现乱码除了第一个hello world!A 请注意在“ hello world”末尾的char A 。 这是因为RandomAccessFile仅在文件中写入字节数组。 情况7将字节写入文件 import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream;public class FileOperationTest {public static void main(String[] args)throws IOException {OutputStream out new FileOutputStream(helloworld.txt);String str hello world!;out.write(str.getBytes());out.close();} } 这次您可以看到“你好世界” 在文件中。 当然您可以逐字节写入OutputStream 但效果不佳 import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream;public class FileOperationTest {public static void main(String[] args)throws IOException {OutputStream out new FileOutputStream(helloworld.txt);String str hello world!;for (byte b : str.getBytes()) {out.write(b);}out.close();} }情况8将字节追加到文件 import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream;public class FileOperationTest {public static void main(String[] args)throws IOException {OutputStream out new FileOutputStream(helloworld.txt, true);String str hello world!;out.write(str.getBytes());out.close();} } 输出 hello world!hello world! 情况9从文件读取字节 import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream;public class FileOperationTest {public static void main(String[] args)throws IOException {InputStream in new FileInputStream(helloworld.txt);byte[] bs new byte[1024];int len -1;while ((len in.read(bs)) ! -1) {System.out.println(new String(bs, 0, len));}in.close();} } InputStream 。 如果到达文件末尾 read将返回-1。 否则它将返回读入缓冲区的字节总数。 情况10复制文件 简单地结合案例7和9 我们将获得复制功能。 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;public class Copy {public static void main(String[] args)throws IOException {if (args.length ! 2) {System.out.println(java Copy SOURCE DEST);System.exit(1);}InputStream input new FileInputStream(args[0]);OutputStream output new FileOutputStream(args[1]);int len 0;byte bs[] new byte[1024];while ((len input.read(bs)) ! -1) {output.write(bs, 0, len);}input.close();output.close();} }情况11将字符写入文件 import java.io.FileWriter; import java.io.IOException; import java.io.Writer;public class FileOperationTest {public static void main(String[] args)throws IOException {Writer out new FileWriter(helloworld.txt);String str hello world!;out.write(str);out.close();} } 对于上述情况您将获得与案例7相同的结果。 那么区别是什么呢 FileWriter用于编写字符流。 它将使用默认的字符编码和默认的字节缓冲区大小。 换句话说为方便起见它是FileOutputStream的包装器类。 因此要自己指定这些值请考虑使用FileOutputStream 。 情况12从文件中读取字符 import java.io.FileReader; import java.io.IOException; import java.io.Reader;public class FileOperationTest {public static void main(String[] args)throws IOException {Reader in new FileReader(helloworld.txt);char cs[] new char[1024];int len -1;while ((len in.read(cs)) ! -1) {System.out.println(new String(cs, 0, len));}in.close();} } 是否使用字节流或字符流 真的要看 两者都有缓冲区。 InputStream / OutputStream提供了更大的灵活性但是会使您的“简单”程序变得复杂。 另一方面FileWriter / FileReader提供了一个整洁的解决方案但是您失去了控制权。 情况13从OutputStream转换为FileWriter import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer;public class FileOperationTest {public static void main(String[] args)throws IOException {Writer out new OutputStreamWriter(new FileOutputStream(helloworld.txt));out.write(hello world!);out.close();} } 您可以指定字符集而不是使用默认字符编码。 例如 Writer out new OutputStreamWriter(new FileOutputStream(helloworld.txt), utf8);情况14从InputStream转换为FileReader import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader;public class FileOperationTest {public static void main(String[] args)throws IOException {Reader in new InputStreamReader(new FileInputStream(helloworld.txt));char cs[] new char[1024];int len -1;while ((len in.read(cs)) ! -1) {System.out.println(new String(cs, 0, len));}in.close();} }案例15使用管道 以下代码创建两个线程一个生产者在一端将某些内容写入管道而另一个消费者从另一端从该管道读取内容。 要创建管道我们需要分别创建PipedInputStream和PipedOutputStream 并使用output.connect(input)或通过其构造函数进行连接。 在此程序中我有意先启动Consumer线程并在启动Producer线程之前让整个程序Hibernate1秒。 这将显示管道确实起作用。 值得注意的是我关闭了Producer中的管道因为“ 写入流的线程应始终在终止之前关闭OutputStream。 ”如果我们删除out.close()行将抛出IOException java.io.IOException: Write end deadat java.io.PipedInputStream.read(PipedInputStream.java:311)at java.io.PipedInputStream.read(PipedInputStream.java:378)at java.io.InputStream.read(InputStream.java:101)at foo.Consumer.run(FileOperationTest.java:58)at java.lang.Thread.run(Thread.java:701)import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream;public class FileOperationTest {public static void main(String[] args)throws IOException, InterruptedException {PipedInputStream input new PipedInputStream();PipedOutputStream output new PipedOutputStream();output.connect(input);Producer producer new Producer(output);Consumer consumer new Consumer(input);new Thread(consumer).start();Thread.sleep(1000);new Thread(producer).start();} }class Producer implements Runnable {private final OutputStream out;public Producer(OutputStream out) {this.out out;}Overridepublic void run() {String str hello world!;try {out.write(str.getBytes());out.flush();out.close();} catch (Exception e) {e.printStackTrace();}} }class Consumer implements Runnable {private final InputStream in;public Consumer(InputStream in) {this.in in;}Overridepublic void run() {byte[] bs new byte[1024];int len -1;try {while ((len in.read(bs)) ! -1) {System.out.println(new String(bs, 0, len));}} catch (IOException e) {e.printStackTrace();}} }情况16将格式化的字符串写入文件 PrintStream添加了一些功能可以方便地打印各种数据值的表示形式。 格式字符串的语法与C几乎相同。 import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream;public class FileOperationTest {public static void main(String[] args)throws IOException {PrintStream print new PrintStream(new FileOutputStream(helloworld.txt));print.printf(%s %s!, hello, world);print.close();} }案例17重定向“标准” IO 在Java中标准输出和错误输出均为PrintStream 。 标准输入是InputStream 。 因此我们可以自由地重新分配它们。 以下代码将标准输出重定向到错误输出。 public class FileOperationTest {public static void main(String[] args) {System.out.println(hello world!);System.setOut(System.err);System.out.println(hello world!);} } 输出在Eclipse中红色文本表示错误消息 hello world!hello world!情况18逐行读取文件 import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;public class FileOperationTest {public static void main(String[] args)throws IOException {BufferedReader reader new BufferedReader(new FileReader(helloworld.txt));String str null;while ((str reader.readLine()) ! null) {System.out.println(str);}reader.close();} }案例19压缩到一个zip文件 import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;public class FileOperationTest {public static void main(String[] args)throws IOException {ZipOutputStream zipOut new ZipOutputStream(new FileOutputStream(helloworld.zip));String str hello world!;for (int i 0; i 3; i) {zipOut.putNextEntry(new ZipEntry(helloworld i .txt));zipOut.write(str.getBytes());zipOut.closeEntry();}zipOut.close();} } 上面的代码创建了一个zip文件并放置了三个文件分别名为“ helloworld0.txt”“ helloworld1.txt”和“ helloworld2.txt”每个文件都包含内容“ hello world”。 案例20从zip文件中提取 import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream;public class FileOperationTest {public static void main(String[] args)throws IOException {ZipInputStream zipIn new ZipInputStream(new FileInputStream(helloworld.zip));ZipEntry entry null;byte bs[] new byte[1024];while ((entry zipIn.getNextEntry()) ! null) {// get file nameSystem.out.printf(file: %s content: , entry.getName());int len -1;// read current entry to the bufferwhile((lenzipIn.read(bs)) ! -1) {System.out.print(new String(bs, 0, len));}System.out.println();}zipIn.close();} } 输出 file: helloworld0.txt content: hello world! file: helloworld1.txt content: hello world! file: helloworld2.txt content: hello world!情况21推回字节 import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PushbackInputStream;public class FileOperationTest {public static void main(String[] args)throws IOException {PushbackInputStream push new PushbackInputStream(new ByteArrayInputStream(hello, world!.getBytes()));int temp 0;while ((temp push.read()) ! -1) {if (temp ,) {push.unread(.);}System.out.print((char) temp);}} } 上面的代码在读取逗号后按了一个点因此输出为 hello,. world! 但是如果您尝试向后推更多字符例如push.unread((...).getBytes()); 您将获得IOException 推回缓冲区已满。 这是因为推回缓冲区的默认大小为1。要指定更大的容量请使用构造函数PushbackInputStream(InputStream in, int size) 例如 import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PushbackInputStream;public class FileOperationTest {public static void main(String[] args)throws IOException {PushbackInputStream push new PushbackInputStream(new ByteArrayInputStream(hello, world!.getBytes()), 10);int temp 0;while ((temp push.read()) ! -1) {if (temp ,) {push.unread((...).getBytes());}System.out.print((char) temp);}} } 输出 hello,(...) world! 参考 简而言之 Java.ioPGuru博客上来自我们JCG合作伙伴 Peng Yifan的 22个案例研究 。 翻译自: https://www.javacodegeeks.com/2013/12/java-io-in-nutshell-22-case-studies.html迁移学习 简而言之
http://www.lebaoying.cn/news/39198.html

相关文章:

  • 自己网站做短链接烟台公司网站定制
  • 做网站用python还是php模板网站和插件有哪些
  • 电子商务网站建设与维护03wordpress安装时需要填写的使用者
  • 在阿里云服务器做淘客网站贵阳市花溪区建设局网站
  • 做网站都需要什么软件企业宣传片
  • 做美瞳网站需要什么资质做智能网站软件
  • 上海网站设计哪家好广州高端网站设计
  • 文字网站居中快速网站模板公司
  • 做网站应下哪个软件卢松松网站怎么做
  • 欧洲网站设计公司注册流程
  • 东莞如何建设网站制作平台去掉wordpress.org
  • 微商城网站建设讯息手机网站建设服务器
  • 徐州做网站需要多少钱网站源码下载 app
  • 网站制作容易吗怎么样外贸产品网站建设
  • 杭州网站建设很 棒网站建设贰金手指下拉壹玖
  • h5响应式网站模板制作杭州品牌网站开发
  • 省级网站 开发建设 资质小红书怎么做关键词排名优化
  • 个人网站推荐东莞最新招聘信息今天
  • 佛山那里有做苗木销售网站广告联盟接单赚钱平台
  • 医生做学分在哪个网站网站内部链接怎麽做
  • 哪个网站专做滨水景观学校网站建设目的是什么
  • 做外贸网站能用虚拟主机吗开发公司app
  • 如何能让企业做网站的打算叫别人做网站权重被转移了
  • 门户网站建设关键点wordpress建壁纸站
  • 有做数学题的网站吗wordpress豆瓣小站
  • 唐山APP小程序网站开发中国优秀的企业网站
  • 学习电子商务网站建设与管理感想深圳网络营销推广渠道
  • 网站备案提交管局深圳网络营销|深圳网站建设公司|专业网络营销运营推广策划公司
  • 网站建设入门教程视频教程杭州网站建设哪家权威
  • 网站建设推广平台有哪些建筑木模板厂家