1. /*//创建一个新文件
  2. public static void main(String[] args) {
  3. File file=new File("D:\\hello.txt");
  4. try {
  5. file.createNewFile();
  6. } catch (IOException e) {
  7.  
  8. e.printStackTrace();
  9. }
  10. }*/
  11.  
  12. /*//File类的两个常量
  13. public static void main(String[] args) {
  14. System.out.println(File.separator);//结果: \
  15. System.out.println(File.pathSeparator);//结果: :
  16. }*/
  17.  
  18. /*//删除一个文件
  19. public static void main(String[] args) {
  20. String fileName="D:"+File.separator+"hello.txt";
  21. File file=new File(fileName);
  22. if(file.exists()){
  23. file.delete();
  24. }else {
  25. System.out.println("文件不存在!");
  26. }
  27. }*/
  28.  
  29. /*//创建一个文件夹
  30. public static void main(String[] args) {
  31. String fileName="D:"+File.separator+"hello";
  32. File file=new File(fileName);
  33. file.mkdirs();
  34. }*/
  35.  
  36. /*//使用listFiles列出指定目录的全部文件
  37. //* listFiles输出的是完整路径
  38. public static void main(String[] args) {
  39. String fileName="D:"+File.separator;
  40. File file=new File(fileName);
  41. File[] str=file.listFiles();
  42. for(int i=0;i<str.length;i++){
  43. System.out.println(str[i]);
  44. }
  45. }*/
  46.  
  47. /*使用isDirectory判断一个指定的路径是否为目录*/
  48. /*public static void main(String[] args) {
  49. String fileName="D:"+File.separator;
  50. File file=new File(fileName);
  51. if(file.isDirectory())
  52. System.out.println("yes");
  53. else {
  54. System.out.println("no");
  55. }
  56. }*/
  57.  
  58. /*列出指定目录的全部内容*/
  59. /*public static void main(String[] args) {
  60. String fileName="D:"+File.separator;
  61. File file=new File(fileName);
  62. print(file);
  63. }
  64. public static void print(File f){
  65. if(f!=null){
  66. if(f.isDirectory()){
  67. File[] fileArray=f.listFiles();
  68. if(fileArray!=null){
  69. for(int i=0;i<fileArray.length;i++){
  70. print(fileArray[i]);//递归调用
  71. }
  72. }
  73. }else {
  74. System.out.println(f);
  75. }
  76. }
  77. }*/
  78.  
  79. /*使用RandomAccessFile写入文件
  80. public static void main(String[] args) throws IOException {
  81. String fileName="D:"+File.separator+"hello.txt";
  82. File file=new File(fileName);
  83. RandomAccessFile randomAccessFile=new RandomAccessFile(file, "rw");
  84. randomAccessFile.writeBytes("1234567890");
  85. randomAccessFile.writeInt(42);
  86. randomAccessFile.writeBoolean(true);
  87. randomAccessFile.writeChar(88);
  88. randomAccessFile.writeDouble(12.678);
  89. randomAccessFile.writeFloat(23.5f);
  90. randomAccessFile.close();
  91. }//如果你此时打开hello。txt查看的话,会发现那是乱码。
  92. */
  93. /*字节流
  94. * 向文件中写入字符串
  95. public static void main(String[] args) throws IOException {
  96. String fileName="D:"+File.separator+"hello.txt";
  97. File file=new File(fileName);
  98. OutputStream outputStream=new FileOutputStream(file);
  99. String string="Hello";
  100. byte[] bs=string.getBytes();
  101. outputStream.write(bs);
  102. outputStream.close();
  103. }*/
  104.  
  105. /**
  106. * 字节流
  107. * 向文件中一个字节一个字节的写入字符串
  108. * @throws IOException
  109. * *//*
  110. public static void main(String[] args) throws IOException {
  111. String fileName="D:"+File.separator+"hello.txt";
  112. File file=new File(fileName);
  113. OutputStream outputStream=new FileOutputStream(file);
  114. String string="Hello GG";
  115. byte[] bs=string.getBytes();
  116. for(int i=0;i<bs.length;i++){
  117. outputStream.write(bs[i]);
  118. }
  119. outputStream.close();
  120. }*/
  121.  
  122. /**
  123. * 字节流
  124. * 向文件中追加新内容:
  125. * @throws IOException
  126. * */
  127. /*public static void main(String[] args) throws IOException {
  128. String fileName="D:"+File.separator+"hello.txt";
  129. File file=new File(fileName);
  130. OutputStream outputStream=new FileOutputStream(file,true);
  131. String string="Hello MM";
  132. //String str="\r\nRollen"; 可以换行
  133. byte[] bs=string.getBytes();
  134. for(int i=0;i<bs.length;i++){
  135. outputStream.write(bs[i]);
  136. }
  137. outputStream.close();
  138. }*/
  139.  
  140. /**
  141. * 字节流
  142. * 读文件内容
  143. * @throws IOException
  144. * */
  145. /*public static void main(String[] args) throws IOException {
  146. String fileName="D:"+File.separator+"hello.txt";
  147. File file=new File(fileName);
  148. InputStream inputStream=new FileInputStream(file);
  149. //byte[] bs=new byte[1024];//预先申请了一个指定大小的空间
  150. byte[] bs=new byte[(int) file.length()];
  151. inputStream.read(bs);
  152. System.out.println("文件长度为:"+file.length());
  153. inputStream.close();
  154. System.out.println(new String(bs));
  155. }*/
  156.  
  157. /**
  158. * 字节流
  159. *读文件
  160. * @throws IOException
  161. * */
  162. /*public static void main(String[] args) throws IOException {
  163. String fileName="D:"+File.separator+"hello.txt";
  164. File file=new File(fileName);
  165. InputStream inputStream=new FileInputStream(file);
  166. byte[] bs=new byte[1024];
  167. int count=0;
  168. int temp=0;
  169. while((temp=inputStream.read())!=(-1)){//当读到文件末尾的时候会返回-1.正常情况下是不会返回-1的
  170. bs[count++]=(byte) temp;
  171. }
  172. inputStream.close();
  173. System.out.println(new String(bs));
  174. }*/
  175.  
  176. /**
  177. * 字符流
  178. * 写入数据
  179. * @throws IOException
  180. * */
  181. /*public static void main(String[] args) throws IOException {
  182. String fileName="D:"+File.separator+"hello.txt";
  183. File file=new File(fileName);
  184. Writer out=new FileWriter(file);
  185. String string="朋友";
  186. out.write(string);
  187. out.close();
  188. }*/
  189.  
  190. /**
  191. * 字符流
  192. * 从文件中读出内容
  193. * @throws IOException
  194. *采用循环读取的方式,因为我们有时候不知道文件到底有多大。
  195. * */
  196. /*public static void main(String[] args) throws IOException {
  197. String fileName="D:"+File.separator+"hello.txt";
  198. File file=new File(fileName);
  199. char[] ch=new char[100];
  200. Reader reader=new FileReader(file);
  201. int temp=0;
  202. int count=0;
  203. while((temp=reader.read())!=(-1)){
  204. ch[count++]=(char) temp;
  205. }
  206. reader.close();
  207. System.out.println(new String(ch, 0, count));
  208. }*/

/**
* 将字节输出流转化为字符输出流
* @throws IOException
* */
/*public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File file=new File(fileName);
Writer out=new OutputStreamWriter(new FileOutputStream(file));
out.write("MM");
out.close();
}*/

/**
* 将字节输入流变为字符输入流
* @throws IOException
* */
/*public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File file=new File(fileName);
Reader reader=new InputStreamReader(new FileInputStream(file));
char[] cs=new char[100];
int len=reader.read(cs);
System.out.println(new String(cs, 0, len));
reader.close();
}*/

/**
* 使用内容操作流将一个大写字母转化为小写字母
* @throws IOException
* 内容操作流一般用来生成一些临时信息的,这样可以避免删除的麻烦。
* */
/*public static void main(String[] args) throws IOException {
String str="ADFGHJKLUYTG";
ByteArrayInputStream inputStream=new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
int temp=0;
while((temp=inputStream.read())!=(-1)){
char ch=(char) temp;
outputStream.write(Character.toLowerCase(ch));
}
String outStr= outputStream.toString();
inputStream.close();
outputStream.close();
System.out.println(outStr);
}*/

1、字节流和字符流的区别:

字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候,是会用到缓冲区的,是通过缓冲区来操作文件的。

使用字节流好还是字符流好呢?

答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

2、文件的复制:

方法一:DOS下就有一个文件复制功能

方法二:用程序

  1. /**
  2. * 文件的复制
  3. * @throws IOException
  4. * 思路:从一个文件中读取内容,边读边写入另一个文件。
  5. * */
  6. public static void main(String[] args) throws IOException {
  7. String helloFileName="D:"+File.separator+"hello.txt";
  8. String rollenFileName="D:"+File.separator+"rollen.txt";
  9. File file1=new File(helloFileName);
  10. File file2=new File(rollenFileName);
  11. if(!file1.exists()){
  12. System.out.println("被复制的文件不存在!");
  13. System.out.println(1);
  14. }
  15. InputStream inputStream=new FileInputStream(file1);
  16. OutputStream outputStream=new FileOutputStream(file2);
  17. if(inputStream!=null&&outputStream!=null){
  18. int temp=0;
  19. while((temp=inputStream.read())!=(-1)){
  20. outputStream.write(temp);
  21. }
  22. }
  23. inputStream.close();
  24. outputStream.close();
  25. }

3、管道流

管道流主要用以进行两个线程之间的通信。

PipedOutputStream 管道输出流

PipedInputStream 管道输入流

  1. /*消息发送类*/
  2. public class Send implements Runnable{
  3. private PipedOutputStream out=null;
  4. public Send() {
  5. out=new PipedOutputStream();
  6. }
  7. public PipedOutputStream getOut(){
  8. return this.out;
  9. }
  10. @Override
  11. public void run() {
  12. String message="hello MM";
  13. try {
  14. out.write(message.getBytes());
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. try {
  19. out.close();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25.  
  26. /*接受消息类*/
  27. public class Recive implements Runnable{
  28. private PipedInputStream input=null;
  29. public Recive() {
  30. input=new PipedInputStream();
  31. }
  32. public PipedInputStream getInput(){
  33. return this.input;
  34. }
  35. @Override
  36. public void run() {
  37. byte[] bs=new byte[1000];
  38. int len=0;
  39. try {
  40. len=this.input.read(bs);
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. try {
  45. input.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. System.out.println("接收的内容是:"+(new String(bs, 0, len)));
  50. }
  51.  
  52. }
  53.  
  54. /*测试类*/
  55. public static void main(String[] args) {
  56. Send send=new Send();
  57. Recive recive=new Recive();
  58. try {
  59. send.getOut().connect(recive.getInput());//管道连接
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. new Thread(send).start();
  64. new Thread(recive).start();
  65. }
  66. }

4、打印流

  1. /* 使用PrintStream进行输出*/
  2. /*public static void main(String[] args) throws IOException {
  3. PrintStream printStream=new PrintStream(new FileOutputStream(new File("D:"+File.separator+"hello.txt")));
  4. printStream.print(true);
  5. printStream.print("GG");
  6. printStream.close();
  7. }*/
  8.  
  9. /*使用PrintStream进行输出
  10. * 并进行格式化*/
  11. /*public static void main(String[] args) throws IOException {
  12. PrintStream printStream=new PrintStream(new FileOutputStream(new File("D:"+File.separator+"hello.txt")));
  13. String name="GG";
  14. int age=26;
  15. printStream.printf("姓名:%s;年龄:%d!",name,age);
  16. printStream.close();
  17. }*/
  18.  
  19. /*使用OutputStream向屏幕上输出内容*/
  20. public static void main(String[] args) {
  21. OutputStream out=System.out;
  22. try {
  23. out.write("MM".getBytes());
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. try {
  28. out.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }

  

java之IO整理(上)的更多相关文章

  1. java之IO整理(下)

    一:对象的序列化 对象序列化就是把一个对象变为二进制数据流的一种方法. 一个类要想被序列化,就行必须实现java.io.Serializable接口.虽然这个接口中没有任何方法,就如同之前的clone ...

  2. java之IO整理(中)

    一:打印流/*System.out.println()重定向输出*/ /*public static void main(String[] args) { System.out.println(&qu ...

  3. java中的IO整理

    写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...

  4. 金九银十,史上最强 Java 面试题整理。

    以下会重新整理所有 Java 系列面试题答案.及各大互联网公司的面试经验,会从以下几个方面汇总,本文会长期更新. Java 面试篇 史上最全 Java 面试题,带全部答案 史上最全 69 道 Spri ...

  5. 【转】 Java中的IO整理

    写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...

  6. Java基础进阶整理

    Java学习笔记整理 本文档是我个人整理的,首先是想通过完成本文档更加扎实自己的基础加强对java语言的理解,然后就是想给入了门的同志们做下贡献. 当然,本文档主要是对java语言基础(当然还有很多基 ...

  7. 尚学堂Java面试题整理

    博客分类: 经典分享   1. super()与this()的差别? - 6 -  2. 作用域public,protected,private,以及不写时的差别? - 6 -  3. 编程输出例如以 ...

  8. 《OD面试》Java面试题整理

    一.面试考察点 1 主语言本身 2 数据库 3 算法 4 Spring/SpringMVC/MyBatis 5 项目经验 1)项目涉及到的技术点深挖: (1)考察候选人技术深度  (2)看候选人遇到问 ...

  9. java学习内容整理

    转自:http://www.cnblogs.com/caoleiCoding/p/6170555.html 首先,我个人比较推崇的学习方法是:先学java前段,也就是HTML,css,js,因为学习j ...

随机推荐

  1. 【PL/SQL编程】循环语句

    1. loop语句 loop plsql_sentence; exit when end_condition_exp; end loop; loop语句会先执行一次循环体,然后再判断“exit whe ...

  2. Kotlin Reference (一) Basic Syntax

    什么是Kotlin Kotlin翻译成中文叫"靠他灵",它是由JetBrains公司发明的一种基于JVM的编程语言,目前Google宣布kotlin为Android开发的官方语言. ...

  3. Freemaker的java.beans.IntrospectionException: type mismatch between read and write methods

    引言:freemaker在特定的spring以及jdk下的问题解决路径. 环境描述 spring 3.1.1, jdk1.8u80, freemake 2.3.19 错误信息描述: 严重: Excep ...

  4. EasyRMS录播管理服务器项目实战:windows上开机自启动NodeJS服务

    本文转自EasyDarwin开源团队成员Penggy的博客:http://www.jianshu.com/p/ef840505ae06 近期在EasyDarwin开源团队开发一款基于EasyDarwi ...

  5. SoftmaxWithLoss函数和师兄给的loss有哪些区别呢

    师兄的: NG教程中提到的:

  6. JAXP使用Stax API时格式化输出XML 2

    之前实现的一个版本:http://www.cnblogs.com/lyhtbc/p/jaxp-pretty-format-validate-validation-stax-stax2.html 这个版 ...

  7. 通过反编译让SpecFlow支持多层属性值的验证

    需求:在使用SpecFlow时,我希望能对目标对象所关联的对象属性进行验证,但SpecFlow(Version 1.9.0)无法实现.如图中红框,可以对专户所属的金融机构的名称进行验证. 反编译步骤 ...

  8. 拷贝ssh公钥到多台服务器上

    这篇文章几乎是对Push SSH public keys to multiple host的翻译,谢谢该作者. 使用SSH登陆.执行命令到远程机器需要输入密码,很多系统需要免输密码访问远程机器,比如h ...

  9. bzoj 5334 数学计算

    bzoj 5334 数学计算 开始想直接模拟过程做,但模数 \(M\) 不一定为质数,若没有逆元就 \(fAKe\) 掉了. 注意到操作 \(2\) 是删除对应的操作 \(1\) ,相当于只有 \(1 ...

  10. Mac OS安装php-redis扩展

    下载php-redis(用于php5.x的版本),地址:https://nodeload.github.com/nicolasff/phpredis/zip/master. 如果是php7.2,选择p ...