java IO的学习

File文件类的使用:

我在桌面放一个文件:10.jpg 路径:C:\\Users\\Administrator\\Desktop\\10.jpg

                  一个文件夹:test 里面放一个文件mysql.txt

public class TestIO {	
  public static void main(String[] args) {
	  File file = new File("C:\\Users\\Administrator\\Desktop\\10.jpg");
	  File file1 = new File("C:\\Users\\Administrator\\Desktop\\test");
	  File file2 = new File(file1,"mysql.txt");
	  System.out.println(file.exists());//文件是否存在
	  System.out.println(file1.exists());
	  System.out.println(file2.exists());
	  System.out.println(file.getAbsolutePath());//获取完全的路径
	  System.out.println(file.getParentFile());//获取上一级路径
	  System.out.println(file.canWrite());//文件是否可写
  }
}

输出结果:

true
true
true
C:\Users\Administrator\Desktop\10.jpg
C:\Users\Administrator\Desktop
true

其实File的构造器还有其他的,我比较常用的是第一个,指定绝对路径的。File类能操作文件本身,但是

不能操作文件的内容啥的,操作流时,要使用到文件,所以先介绍了下文件。

1、字节流:

InputStream:输入字节流,一个字节为8位,用于视频、图片等文件的操作。

典型的实现类:FileInputStream

在桌面上创建一个文件,my.txt 里面写入:hello,my name is cjh....

public class TestIO2 {	
  public static void main(String[] args) {
	  //hello,my name is cjh....
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my.txt");
	  InputStream in   = null;
	  try {
		  in           = new FileInputStream(fileIn);
		  byte[] b     = new byte[200];
		  int len      = 0;
		  while((len = in.read(b))!=-1) {
			   String result = "";
			   result        = new String(b);		   
			   System.out.print(result);
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			in.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }	  
  }
}

可以看到控制台打印:hello,my name is cjh....

这说明已经把文件中的内容读进来了。

OutputStream:输出字节流

典型的实现类:FileOutputStream.

public class TestIO3 {	
  public static void main(String[] args) {
	  //hello,my name is cjh....
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\my1.txt");
	  OutputStream out   = null;
	  try {
		  out           = new FileOutputStream(fileOut);
		  out.write("hello,i am cjh...".getBytes());
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			out.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }	  
  }
}

执行代码之后,会在桌面生成一个my1.txt文件,打开可以看到: hello,i am cjh...


这里做一个综合的练习,将桌面的文件10.jpg写到test目录下:

public class TestIO {
  public static void main(String[] args) {
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\10.jpg");
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\test\\"+fileIn.getName());
	  InputStream in   = null;
	  OutputStream out = null;
	  try {
		  //读入输入流
		  in = new FileInputStream(fileIn);
		  byte[] b  = new byte[1024];
		  int lent  = 0;
		  //指定输出流
		  out       = new FileOutputStream(fileOut);
		  while((lent=in.read(b))!=-1) {
			  out.write(b, 0, lent);
		  }
	  } catch (Exception e) {
		  e.printStackTrace();
	  }finally {
		  //记住要关闭流
		  try {
			in.close();
			 out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	 
	  }
  }
}
要指定输出文件的具体路径和名称,那么会将输入文件的内容,写入到输出文件,则相当于实现了文件的复制功能。


2、字符流

Reader:输入字符流;用于文本文件的传输,因为一些字符可能是两个字节的,比如中文。

典型的实现:FileReader

在桌面添加一个my2.txt文件,写入:本来讨厌下雨的天空,突然听见有人说爱我。

public class TestIO4 {	
  public static void main(String[] args) {	  
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my2.txt");
	  Reader reader   = null;
	  try {
		  reader           = new FileReader(fileIn);
		  char[] c  = new char[1024];
		  int len   = 0;
		  while((len=reader.read(c))!=-1) {
			  String result = new String(c, 0, len);
			  System.out.println(result);
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			reader.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }		  
  }
}
执行程序,控制台输出:本来讨厌下雨的天空,突然听见有人说爱我。

这里我再用上述的字节输入流来读中文和英文都有的文本,看看出现什么问题:

在桌面的my.txt文件中,将内容改为:a最美的不是下雨天,而是与你躲过雨的屋檐。

public class TestIO2 {	
  public static void main(String[] args) {
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my.txt");
	  InputStream in   = null;
	  try {
		  in           = new FileInputStream(fileIn);
		  byte[] b     = new byte[4];
		  int len      = 0;
		  while((len = in.read(b))!=-1) {
			   String result = "";
			   result        = new String(b);		   
			   System.out.print(result);
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }	  
  }
}

输出结果:a最?赖牟皇窍掠晏欤怯肽愣愎甑奈蓍堋i堋

当然,如果也能读到时正确的字符,如果字节数刚好匹配的话。试着改成:aa最美的不是下雨天,而是与你躲过雨的屋檐

再执行程序,就会发现是正确的输出。

当然字符流就不会出现这个问题,它才不管你几个字节,就是按照一个字符来处理,读的时候就是读的字符。


Writer:字符输出流。

典型实现:FileWriter

public class TestIO5 {	
  public static void main(String[] args) {	  
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\my3.txt");
	  Writer writer   = null;
	  try {
		  writer           = new FileWriter(fileOut);
		  writer.write("听妈妈的话,别让她受伤。");
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			  writer.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }	  
  }
}
执行程序,你会在桌面上发现创建了一个my3.txt的文件,打开,里面的内容为:听妈妈的话,别让她受伤。

做一个练习,将刚才桌面上的my2.txt文件写入到test目录下。

public class TestIO6 {
	public static void main(String[] args) {	
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my2.txt");
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\test\\my2.txt");
	  Reader reader   = null;
	  Writer writer   = null;
	  try {
		  reader           = new FileReader(fileIn);
		  writer           = new FileWriter(fileOut);
		  char[] c  = new char[2];
		  int len   = 0;
		  while((len=reader.read(c))!=-1) {
			 writer.write(new String(c));
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
			reader.close();
			writer.close();
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }		  
  }
}
执行程序,在test目录下会有my2.txt文件生成,同时内容和桌面的文件是一样的。


3、缓冲流

public class TestIO7 {
	public static void main(String[] args) {	
	  File fileIn      = new File("C:\\Users\\Administrator\\Desktop\\my2.txt");
	  File fileOut      = new File("C:\\Users\\Administrator\\Desktop\\test\\my3.txt");	
	  BufferedReader bufReader = null;
	  BufferedWriter bufWriter = null;
	  try {		 
		  bufReader        = new BufferedReader(new FileReader(fileIn));
		  bufWriter        = new BufferedWriter(new FileWriter(fileOut));
		  char[] c  = new char[2];
		  int len   = 0;
		  while((len=bufReader.read(c))!=-1) {
			  bufWriter.write(new String(c));
		  }
	  } catch (Exception e) {
		e.printStackTrace();
	  }finally {
		  try {
		   if( bufWriter != null && bufReader != null ) {	
				bufWriter.close();
				bufReader.close();
		   }			
		  } catch (IOException e) {
			e.printStackTrace();
		  }
	  }	  
  }
}

执行程序,在test目录下会生成my3.txt文件,内容和my2.txt一样。

猜你喜欢

转载自blog.csdn.net/chenjianhuideyueding/article/details/79397017