java io+序列化



public class FileTest {
public static void main(String[] args) {
System.out.println(File.separator);//----\
System.out.println(File.pathSeparator);//----;
File file = new File("H:\\TDDOWNLOAD");
print(file);
}
/*搜索指定目录的全部内容[包括递归]*/
 public static void print(File f){
       if(f!=null){
           if(f.isDirectory()){
            File[] fileArray = f.listFiles();
            for (int i = 0; i < fileArray.length; i++) {
            print(fileArray[i]);//是目录则递归调用
}
           }else{
               System.out.println(f);
           }
       }
   }


public void createFile() throws IOException{
/* file类有两个长量 */
String sepator = File.separator;//----/
String pathSeparator = File.pathSeparator;//-----;
String fileName = "D:"+sepator+"hello.text";

File file = new File(fileName);

/*创建文件*/
file.createNewFile();

/*删除文件*/
if(file.exists()){
file.delete();
}else{
System.out.println("文件不存在");
}

/*创建文件夹*/
file.mkdir();

/*使用list列出目录的全部文件名(包括隐藏文件)*/
String[] fileNames = file.list();//f.listFiles()列出完整路径
for (int i = 0; i < fileNames.length; i++) {
System.out.println(fileNames[i]);
}

/*判断一个指定的路径是否为目录*/

if(file.isDirectory()){//判断的一个目录是否为目录
System.out.println("这是一个文件夹");
}
//----------------------------------写文件--------------------------------

String content = "写入文件的内容";

/*使用RandomAccessFile写入文件*/
RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");//这里是权限
randomAccessFile.writeBytes("用RandomAccessFile写入文件");

/*字节流【不会用到缓冲区】
* OutputStream  。。。。。 。outputStream向文件中写入字符串*/
OutputStream out = new FileOutputStream(file,true);//有true代表追加
out.write(content.getBytes());
out.close();


/*字符流【用缓存区】
* 用Writer【向文件写入数据】*/
Writer outwriter = new FileWriter(file);
outwriter.write(content);
outwriter.close();


//-----------------------------------读文件------------------------------
/*字节流   【不会用到缓冲区】
* 读文件内容,节省空间*/
InputStream in = new FileInputStream(file);
byte[] contentByte = new byte[1024];
int count = 0;
int temp;
while((temp = in.read(contentByte))!=-1){
contentByte[count++] = (byte)temp;
};
System.out.println(new String(contentByte));
in.close();



/*字符流【用缓存区】
* 读文件*/
char[] ch = new char[100];
Reader reader = new FileReader(file);
//一次读完,读入的长度【此时的ch变成了读完后的字符数组】
int readInt = reader.read(ch);//这做的风险在于如果文件过大,则可能内存不足,所以还可以循环读取
String readContent = new String(ch,0,readInt);
System.out.println("内容是:"+readContent);
//下面是循环读取
int temp1 = 0;
int connt1 = 0;
while((temp1 = reader.read())!=-1){
ch[connt1++] = (char) temp1;
}
System.out.println(new String(ch,0,connt1));


}






 
 //复制文件:
public void copyFile(File sourceFile,File destinFile) throws IOException{
 
InputStream in = new FileInputStream(sourceFile);
 
OutputStream out = new FileOutputStream(destinFile);
 
byte[] everyTimeRead = new byte[1024];
int counter = 0;
int temp = 0;
while((temp = in.read(everyTimeRead))!=-1){
// everyTimeRead[counter++] = (byte) temp;
out.write((byte) temp);
}
in.close();
out.close();
 
}

}

1
2
3
4
5
6
7
8
/**
  * 取得本地的默认编码
  * */
public  class  CharSetDemo{
     public  static  void  main(String[] args){
         System.out.println( "系统默认编码为:"  + System.getProperty( "file.encoding" ));
     }
}

【运行结果】:

系统默认编码为:GBK


=====================================有关序列化=============================================

对象的序列化
定义:
对象序列化就是把一个 对象 变为 二进制数据流 的一种方法。

为什么要序列化:

       用于将内存中对象(可以理解为程序的配置状态)保存到文件或数据库,待需要时恢复

或者将其在网络上传输给远程主机使用。

如何序列化:

一个类要想被序列化,就行必须实现java.io.Serializable接口。虽然这个接口中没有任何方法,就如同之前的cloneable接口一样。

实现了这个接口之后,就表示这个类具有被序列化的能力。


为什么一般用的少?

因为我们一般都在做web开发,数据一般都知道通过orm工具保存到了数据库中,很少有把对象数据保存到文件中的,所以接触比较少。其实挺有用

比如EJB远程调用网络传输的时候需要序列化

比如EJB远程调用分布式存储,缓存存储等

1.当对象需要被网络传输时
2.对象状态需要被持久化时




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的IO序列是指将一个Java对象转换为字节流以便存储或传输,也可以将字节流转换回Java对象。序列可以用于网络通信,分布式系统、数据库存储等场景。 Java中的序列是通过实现Serializable接口来实现的,在这个接口中没有定义任何方法,仅仅起到了一个标识作用。实现了Serializable接口的对象可以被序列为字节流,然后再通过反序列操作将其转换回对象。 Java中的序列可以使用ObjectOutputStream和ObjectInputStream类来实现。ObjectOutputStream可以将一个对象序列为字节流并输出到文件或网络流中,而ObjectInputStream可以将字节流反序列为对象。 下面是一个简单的Java序列示例: ```java import java.io.*; public class SerializationDemo { public static void main(String[] args) { // 创建一个Person对象 Person person = new Person("Tom", 20); // 将Person对象序列到文件中 try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"))) { out.writeObject(person); } catch (IOException e) { e.printStackTrace(); } // 从文件中读取Person对象 try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"))) { Person p = (Person) in.readObject(); System.out.println(p); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } class Person implements Serializable { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } ``` 在上面的示例中,我们创建了一个Person类,并实现了Serializable接口。然后将一个Person对象序列到文件中,并从文件中读取并反序列为Person对象。最后输出反序列得到的Person对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值