java基础(9)

文件对象

File:文件和目录路径名的抽象表达形式

文件类:普通文件,目录文件

文件类的构造方法:特定路径+文件名

例:创建普通文件

Public class  TestFile{

 File file;

 Public TestFile(){

  File = new File(“c:/a.txt”);

//判断文件是否存在

  Boolean b= file.exists();

  If(!b){

//如果文件不存在,创造出一个在此路径下的文件,此方法返回为true时,代表创建成功

  Try{

File.createNewFile();

}catch(IOException e){

 e.printStackTrace();

}

}

}

  Publicstatic void main(String[] args){

  New TestFile();

}

}

Mkdir():创建此抽象路径名指定的目录

例:创建目录文件(相当于创建了一个叫a.txt的文件夹)

Public class TestFile{

 File file ;

 Public TestFile(){

  File = new File(“c:/a.txt”);

  Boolean b = file.exits();

  If(!b){

  File.mkdir();

}

}

Public static void main(String[] args){

  New TestFile();

}

}

输入输出流:

往文件里写入数据/读取文件里的数据

类FileOutputSteam:计算机将数据流输出到文件中

 

例:

Public TestFile{

File file;

Public TestFile(){

  File = new File(“c:/a.txt”);

  Boolean b = file.exits();

  If(!b){

  Try{

  File.createNewFile();

}catch(IOException e){

  e.printStackTrace();

}

}

Try{

FileOutputStream fos = new FileOutputStream(file);

String info = “hello world”;

//写入方法只能写入字节,所以将字符串转换成字节

Fos write(info.getBytes());

  }catch (FileNotFoundExceptione){

    e.printStackTrace();

}catch(IOException e){

  e.printStackTrace();

}

}

Public static void main(String[] args){

  New TestFile();

}

}

输入输出流二:

FileInputStream

例:

Public class TestReadFile{

  Public voidreadFile(String filePath){

Try{

FileInputStream fis = new FileInputStream(filePath);

 

Byte[] b = new byte[25];

Fis.read(b);

System,out,println(new String(b));

}catch(FileNotFoundException e){

  e.printStackTrace();

}catch(IOException e){

  e.printStackTrace();

}

}

 

  Publicstatic void main(String[] args){

  TestReadFile obj = new TestReadFile();

  Obj.readFile(“c:/a.txt”);

}

}

输入输出流三:

字符输入流:Bufferedreader,从字符输入流中读取文本(一个字符是两个字节)--

字符输入流的方法:readline():读取一行

InputSteamReader:是字节流通向字符流的桥梁

FileInputReader读数据或FileOutputReader写数据时,都只能用byte字节来读写数据

例:

Public class TestReadFile{

  Publicvoid readFile(String filePath){

Try{

  FileInputStream fis = new FileInputStream(filePath);

//先将FileInputStream转换成InputStreamReader,再将InputStreamReader转换成BufferedReader

  Bufferedreader br =new BufferedReader(new InputStreamReader(fis));

//读取一行,且文件指针自动指向下一行

  String info =br.readline();

  System.out.println(info);

}catch(FileNotFoundExceptione){

  e.printStackTrace();

}catch(IOExceptione){

  e.printStackTrace();

}

}

Public static void main(){

  TestReadFile obj = new TestReadFile();

  Obj.readFile(“c:/a.txt”);

}

}

输入输出流四:

字符输出流:PrintWriter

Public class TestPrintWriter{

  Publicstatic void main(String[] args){

Try{

  PrintWriter pw = newPrintWriter(“c:/a.txt”);

  Pw.println(“helloworld”);//写到一个缓冲区

  Pw.flush();//将缓冲区的数据刷新到文件中

  /*或直接关闭这个流,数据将自动更新到文件中去

Pw.close();

*/

}catch(FileNotFoundExceptione){

  e.printStackTrace();

}

}

}

输入输出流五:

标准流:标准输入流,标准输出流,标准错误流

类System:

  System.out:标准输出流

 System.in:标准输入流—system.in.read(字节);

 System.err:标准错误输出流

输入输出流六:

对象流:ObjectOutputStream和ObjectinputStream

保存对象到文件,去文件里读取对象

例:

先写一个简单的javabean类:

Public class Usersimplements java.io.Serializable{

//要被写入的类必须序列化

  PrivateString username,password;

 

 Public String getUsername(){

  Return username;

}

Public void setUsername(String username){

  This.username = username;

}

Public String getPassword(){

  Return password;

}

Public void setPassword(String password){

  This.password = password;

}

}

再写一个读写类:

Public class TestObject{

  Publicstatic void main(String[] args){

/*写入类:将一个对象写入文件中

Try{

FileOuputStream oos = new FileOutputStream(“c:/a.txt”);

Users user1 =new Users();

User1.setUsername(“cindy”);

User1.setPassword(“123456”);

 

ObjectOutputStream objs = newObjectOutputStream(oos);

Objs.writeObject(user1);

}catch(FileNotFoundExceptione){

  e.printStackTrace();

}catch(IOExceptione){

  e.printStackTrace();

}

*/

/*读类:将文件中的对象读出

Try{

  FileInputStream fis =new FileInputStream(“c:/a.txt”);

  ObjectInputStream ois= new ObjectInputStream(fis);

  Users user1 =(Users)ois.readObject();

 System.out.println(user1.getUsername()+”:”+user1.getPassword());

}catch(FileNotFoundExceptione){

  e.printStackTrace();

}catch(IOExceptione){

  e.printStackTrace();

}

*/

}

}

随机访问文件类RandomAccessFile:

文件类,封装了流的对象,而且提供了一个缓冲区(字符数字),同时提供了一个指针(可以从缓冲区指向某些特定地方的数据来读取或写入)

可以读也可以写

例:

Public class TestRandomAccessFile{

  Publicstatic void main(String[] args){

  Try{

   //构造方法:文件路径,模式(可读,可写,可读可写)

   RandomAccessFile raf = new RandomAccessFile(“c:/a.txt”,”rw”);

    String s= raf,readline();

    System.out.println(s);

}catch(FileNotFoundException e){

  e.printStackTrace();

}catch(IOException e){

  e.printStackTrace();

}

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值