java io+序列化
发布日期:2021-10-05 07:45:45 浏览次数:2 分类:技术文章

本文共 3432 字,大约阅读时间需要 11 分钟。

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.对象状态需要被持久化时

转载地址:https://blog.csdn.net/lixld/article/details/37918401 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java模式
下一篇:今天总结下memcached

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月11日 22时13分17秒