Java文件有话说

版权声明:翀版 https://blog.csdn.net/biggerchong/article/details/82820567

各种编程语言对文件的基本操作都是:

  1. 创建/删除文件
  2. 读写改文件的内容
  3. 按要求筛选文件名/文件内容
  4. 配置文件的使用

Java语言不同与C++的是java在对文件的读写改操作时必须转换为byte字节来存入文件。通俗的来说就是在java中想要将int、String、double等基本类型/应用类型(对象)存入文件中,必须先转换为byte字节,然后才能调用java.io*、java.util*包中的方法等来具体的实现;同样的从文件中取出数据也是先对byte操作,从byte中取出响应的字节来进一步的转换为我们需要的各种类型。


问:如果我们想将int、String、Students(类对象)存入文件中,该怎么办?

这是就要注意了,因为不同的类型都要先转换为byte型,然后存入文件中,但是由于存入的类型各不相同,这就会让从文件中取数据变得困难起来,这里对于字符串采用Length + Content编码,即长度——数据,意思就是在存入String类型前先把String的byte数记录下来并存入文件中,这样从文件中读取String时就先读取字节数,然后取出响应长度的字节出来,最后再将byte[ ]转换为String。

//写入文件
byte[ ] buffer=new byte[1024];

ByteBuffer dstbuff=ByteBuffer.wrap(buffer);

dstbuff.putInt( );

String str=”你好,中国!”;

byte[ ] buf=str.getByte( );

int length=buf.length;

dstbuff..putShort((short)length);

dstbuff..put(buf);

File filename=new File(“e:\\example\\abc.txt”);  //并没有创建File,只是定义了一个filepath

FileOutputStream file=new FileOutputStream(filename);

file.write(buf);

file.close( );



//读出文件
byte[ ] buffer=new byte[1024];

File filename=new File(“e:\\example\\abc.txt”);

FileInputStream file=new FileInputStream(filename);

file.read(buffer);

file.close( );

ByteBuffer dstbuff=ByteBuffer.wrap(buffer);

int id=dstbuff.getInt( );

short length=dstbuff.getShort( );

byte[ ] buf=new byte[length];

dstbuff.get(buf,0,length);

String name=new String(buf,”GBK”);

配置文件的使用

Eclipse中自带一个properties的配置文件,该文件可以主要形式为:key=data,通过创建一个Properties对象来读取出来properties文件中key所对应的data数据。

//config.properties文件

title=Celsius--and--Fahrenheit
range.from=1
range.to=30

//temp.java

public class temp
{
    private String title;
    private int from;
    private int to;
    
    public temp(String title, int from, int to)
    {
        this.title = title;
        this.from = from;
        this.to= to;
    }
    
    public void display()
    {
        // 打印 "摄氏度 - 华氏度" 转换表
        System.out.println(title);
        for(float c= from; c<=to; c+=1)
        {
            float f = 32 + c * 9 / 5;
            String line = String.format("%.0f - %.1f", c, f);
            System.out.println(line);;
        }
    }
}



//hello.java

import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;

public class Hello
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        Properties proper=new Properties();
        try
        {    
            //查找并读取properties配置文件

            InputStream file=Hello.class.getResourceAsStream("/config.properties");  
            proper.load(file);  //将配置文件读取到Properties对象中来
            file.close();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            System.out.println("InputStream load is false!");
        }


        //取出key——>data
        String title=proper.getProperty("title");
        String From=proper.getProperty("range.from", "1");
        String To=proper.getProperty("range.to", "50");
        int from=Integer.valueOf(From);
        int to=Integer.valueOf(To);
        
        temp ex=new temp(title,from,to);
        ex.display();
    }

}

     当然更为广泛应用的配置文件还是xml配置文件了,java中含有调用xml文件的包,利用这些包可以实现对xml文件中的<root>下的子元素<child>的读取了。在这里就不做介绍了,之后会继续更新xml配置文件的读取与写入。
 

猜你喜欢

转载自blog.csdn.net/biggerchong/article/details/82820567