java(20130805)迭代器、资源文件、国际化、、I/O流

来源:互联网 发布:大屏幕控制软件 供应商 编辑:程序博客网 时间:2024/04/30 09:13

迭代器:

调用集合iterator()方法生成迭代器

Hasnext()判断迭代器是否有后续元素。

Next()取除当前元素,将指针指向下个元素

 

资源文件:

后缀名:properties

Name = value形式等价于map

 

Eg

    publicstaticvoid main(String[] args)throws IOException {

//     Properties prop = new Properties();

//     FileInputStream fin = new FileInputStream(newFile("E:\\workspace\\test20130805\\src\\com\\etc\\testproperties\\myproperties.properties"));\\文件的物理存放路径

//     prop.load(fin);

//     Set<Object> ss = prop.keySet();

//     for(Object o:ss){

//         System.out.println(o.toString());

//         System.out.println(prop.get(o));

//     }

//    

       List<String> ss = new ArrayList<String>();

       ss.add("str1");

       ss.add("str2");

       ss.add("str3");

       Iterator<String> is = ss.iterator();

       while(is.hasNext()){

           String str = is.next();

           System.out.println(str);

       }

      

    }


 

Properties文件:

 

 

 

国际化

资源文件名_语言(zh,en)_国家(CN,US).properties

System.out.println(Locale.getDefault());

       System.out.println("请输入语言:");

       Scanner sc = new Scanner(System.in);

       String str = sc.nextLine();

       Locale.setDefault(new Locale(str));//指定区域语言 eg:zh_CN

       ResourceBundle rb = ResourceBundle.getBundle("com.etc.testproperties.myproperties",Locale.getDefault());//工程中相对路径

       System.out.println(rb.getObject("com.myappname"));


文件操作类(java.io.File)

见JDKAPI(File)

 

 

I/O流:

字节流:InputStream,OutputStream

字符流:InputStreamReader,OutputStreamWriter

缓冲区流:BufferedReader;BufferedWriter

具体实现

文件字节流:FileInputStream, FileOutputStream

对象流: ObjectInputStream, ObjectOutputStream

 

文件字符流:FileReaderFileWriter

 

 

 

缓冲区:

购物车

对磁盘的操作比对内存的操作花费更多的时间

先把内容写到缓冲中,等满了以后一同写到磁盘上,效率高.

如果想强制缓冲区立即写入可以使用flush

 

如果把一个类写到文件中或者网络上传输时该怎么办呢?

当我们的对象需要写到文件中时我们需要使类实现Serializable接口.

主要涉及到的类是ObjectInputStream,ObjectOutputStream

注意:序列化可能会降低程序的性能.

原创粉丝点击