JavaSE_IO流

本文详细解析了Java中FileWriter类的相关方法,包括read、write等操作,并演示了如何进行文件读写、换行及异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FileWriterDemo1

package IO;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;
public class FileWriterDemo1 {
	// 类 FileWriter的方法全是继承的!
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	public void method11() {
		/*		public int read(char[] cbuf, int offset, int length)
        throws IOException将字符读入数组中的某一部分。 
   类 Reader 中的 read
   cbuf - 目标缓冲区
   offset - 从其处开始存储字符的偏移量
   length - 要读取的最大字符数 
   返回:读取的字符数,如果已到达流的末尾,则返回 -1 
   抛出: IOException - 如果发生 I/O 错误
       */   
   }
   @Test
   public void method10() throws IOException {
      /*public int read(char[] cbuf)
               throws IOException将字符读入数组。
               在某个输入可用、发生 I/O 错误或者已到达流的末尾前,
               此方法一直阻塞。 
      参数:      cbuf - 目标缓冲区 
      返回:      读取的字符数,如果已到达流的末尾,则返回 -1 
      抛出:       IOException - 如果发生 I/O 错误*/      
      //abc.txt放在src目录下,抛异常:系统找不到指定文件
      //abc.txt放在src\IO包下,仍然抛异常:系统找不到指定文件
      //千万注意:abc.txt必须放在该工程的根目录下面:
      FileReader fr=new FileReader("abc.txt");
      char[] buf=new char[3];
      int length=0;
      while((length=fr.read(buf))!=-1){
         System.out.print(new String(buf, 0, length));
      }
      //E:\javase\webwork\source_code\abc.txt
      //意思就是:abc.txt 位于java工程的根目录下面~
   }
   @Test
   public void method9() throws IOException {
      // 用Reader中的read方法读取字符(单个单个地读)(不提倡)
/*      public int read()
               throws IOException读取单个字符。 
      覆盖:      类 Reader 中的 read
      返回:   读取的字符,如果已到达流的末尾,则返回 -1 
*/
      FileReader fr=new FileReader("abc.txt");
      //单个单个地读(不提倡)
      int ch=0;
      while ((ch=fr.read())!=-1) {
         System.out.print((char)ch);
      }
      //E:\javase\webwork\source_code\abc.txt
      //意思就是:abc.txt 位于java工程的根目录下面~
   }
   @Test
   public void method8() {
      // IOE异常处理机制
      FileWriter fw=null;
      try {
         //FileNotFoundException: k:\abc.txt (系统找不到指定的路径。)
         //fw=new FileWriter("k:\\abc.txt");
         //会自动在java工程的根目录创建文档
         //如果文件已存在,会强行覆盖~
         fw=new FileWriter("abcd.txt");
         // System.getProperty("line.separator");
         fw.write("abc"+LINE_SEPARATOR+"中国");
         //换行方法4,使用\n\r回车换2行!
      } catch (IOException e) {
         e.printStackTrace();
      }finally{
            try {
               fw.close();
            } catch (IOException e) {
               //底层问题,解决不了,只有抛~
               throw new RuntimeException("关闭失败!");
            }
      }
   }
   //换行方法5,使用环境变量换行
   @Test
   public void method7() throws IOException {
      //会自动在java工程的根目录创建文档
      //如果文件已存在,会强行覆盖~
      FileWriter fw=new FileWriter("abcd.txt");
      // System.getProperty("line.separator");
      fw.write("abc"+LINE_SEPARATOR+"中国");//换行方法4,使用\n\r回车换2行!
      fw.close();
   }
   @Test
   public void method5() throws IOException {
      //换行方法3
      FileWriter fw=new FileWriter("abc.txt");
      fw.write("abc\r\n中国");//换行方法3,使用\r\n回车换行!
      fw.close();
      //注意 windows使用\n\r   \n   \r都无法换行,唯有\r\n
   }
   @Test
   public void method2() throws IOException {
      // append,true 会在后面加入文本!
      FileWriter fw=new FileWriter("abc.txt", true);
      fw.write("abc中国");
      fw.close();
      //操作已经关闭的流无效!IOException
      //fw.write("abc中国");//操作已经关闭的流无效!IOException
   }
   @Test
   public void method1() throws IOException {
      /*      从类 java.io.OutputStreamWriter 继承的方法 
      close, flush, getEncoding, write, write, write 
        从类 java.io.Writer 继承的方法 
      append, append, append, write, write 
        从类 java.lang.Object 继承的方法 
      clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait 
       */
      //默认在java工程的根目录:E:\javase\ework\source_code
      FileWriter fw=new FileWriter("abc.txt");
      fw.write("abc");//3字节
      fw.write("中国");//现在是7字节,1个汉字占两个字节
      fw.flush();//默认关流的时候,会自动flush (或者缓冲区8K满的时候)
      fw.close();
   }
}
未完待续...


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值