一、File类

1、file既可以代表一个特定文件的名称,又可以代表一个目录下的一组文件的名称,实际上,FilePath对这个类来说是个更好的名字.
2、目录列表器例:

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern; public class DirList {
public static void main(String[] args) {
File path = new File("./data/baiduvideo/");
String[] list;
if (args.length == 0)
list = path.list();
else {
list = path.list(new DirFilter(args[0]));
}
Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);//按字母排序
for(String dirItem : list)
System.out.println(dirItem);
}
}
class DirFilter implements FilenameFilter {
private Pattern pattern;
public DirFilter(String regex){
pattern = Pattern.compile(regex);
}
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
}
//这是list方法的内部实现,策略设计模式
/*public String[] list(FilenameFilter filter) {
String names[] = list();
if ((names == null) || (filter == null)) {
return names;
}
ArrayList v = new ArrayList();
for (int i = 0 ; i < names.length ; i++) {
if (filter.accept(this, names[i])) {
v.add(names[i]);
}
}
return (String[])(v.toArray(new String[v.size()]));
}*/
//匿名内部类改写,可读性不好
public class DirList {
public static void main(final String[] args) {
File path = new File("./data/baiduvideo/");
String[] list;
if (args.length == 0)
list = path.list();
else {
list = path.list(new FilenameFilter() {
private Pattern pattern = Pattern.compile(args[0]);
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
}
Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);//按字母排序
for(String dirItem : list)
System.out.println(dirItem);
}
}

File类其它常见用法略.

二、io基本使用

package testio;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader; public class BufferedInputFile {
//1、缓冲输入文件
public static String read(String filename) throws IOException{
//BufferedReader目的是为了提高性能
BufferedReader in = new BufferedReader(new FileReader(filename));
String s;
StringBuilder sb = new StringBuilder();
while((s=in.readLine())!=null)
sb.append(s);
sb.append("\n");//readLine已经将换行符删了
in.close();
return sb.toString();
}
public static void main(String[] args) throws IOException {
System.out.println(read("E:/eclipse/testnginx/src/testio/BufferedInputFile.java"));
}
}
public class MemoryInput {
//2、从内存输入
public static void main(String[] args) throws IOException {
StringReader in = new StringReader(
BufferedInputFile
.read("E:/eclipse/testnginx/src/testio/MemoryInput.java"));
int c;
while ((c = in.read()) != -1)
System.out.print((char) c);
}
}
public class FormattedMemoryInput {
//3、格式化内存输入--按字节读取字符,防止出现乱码
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(
BufferedInputFile.read(
"E:/eclipse/testnginx/src/testio/MemoryInput.java")
.getBytes()));
while(true)
System.out.print((char)in.readByte());
}
}
public class BasicFileOutput {
//1、基本文本输出,为了提供格式化机制,使用PrintWriter
/*static String file = "BasicFileOutput.out";
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(
"E://eclipse//testnginx//src//testio//BasicFileOutput.java"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
file)));
String s;
int lineCount = 1;
while ((s = in.readLine()) != null)
out.println(lineCount++ + ":" + s);
out.close();
in.close();
}*/
//方式2从内存输入
static String file = "BasicFileOutput.out";
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new StringReader(
BufferedInputFile.read("E:/eclipse/testnginx/src/testio/BasicFileOutput.java")));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
file)));
//PrintWriter out = new PrintWriter(file); //PrintWriter的一种快捷方式
String s;
int lineCount = 1;
while ((s = in.readLine()) != null)
out.println(lineCount++ + ":" + s);
out.close();
in.close();
}
}

javase(9)_java io系统的更多相关文章

  1. 从零开始山寨Caffe·拾贰:IO系统(四)

    消费者 回忆:生产者提供产品的接口 在第捌章,IO系统(二)中,生产者DataReader提供了外部消费接口: class DataReader { public: ......... Blockin ...

  2. 从零开始山寨Caffe·陆:IO系统(一)

    你说你学过操作系统这门课?写个无Bug的生产者和消费者模型试试! ——你真的学好了操作系统这门课嘛? 在第壹章,展示过这样图: 其中,左半部分构成了新版Caffe最恼人.最庞大的IO系统. 也是历来最 ...

  3. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  4. io系统

    一.浅谈io系统 io系统的结构化思想是:输入-转换流-装饰器-输出. 对于字节流来说,常见的结构类为: package com.handchina.yunmart.middleware.servic ...

  5. 彻底明白Java的IO系统

    java学习:彻底明白Java的IO系统 文章来源:互联网 一. Input和Output1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源.在Java的IO中,所有 ...

  6. 什么是PROFINET IO系统的实时性

    实时系统是指系统能及时响应外部事件的请求,在规定的时间内完成对该事件的处理,并控制所有实时任务协调一致的运行. PROFINET IO系统的实时性就是指当有一个外部事件发生时,从输入信号到传输.到控制 ...

  7. Java的IO系统

     Java IO系统     "对语言设计人员来说,创建好的输入/输出系统是一项特别困难的任务."     由于存在大量不同的设计方案,所以该任务的困难性是很容易证明的.其中最大的 ...

  8. 【Java基础系列】Java IO系统

    前言 创建好的输入/输出系统不仅要考虑三种不同种类的IO系统(文件,控制台,网络连接)还需要通过大量不同的方式与他们通信(顺序,随机访问,二进制,字符,按行,按字等等). 一.输入和输出 Java的I ...

  9. Atitit.软件开发概念说明--io系统区--特殊文件名称保存最佳实践文件名称编码...filenameEncode

    Atitit.软件开发概念说明--io系统区--特殊文件名称保存最佳实践文件名称编码...filenameEncode 不个网页title保存成个个文件的时候儿有无效字符的问题... 通常两个处理方式 ...

随机推荐

  1. SQL 日期函数转换

    1.转换函数 与date操作关系最大的就是两个转换函数:to_date(),to_char() to_date() 作用将字符类型按一定格式转化为日期类型: 具体用法:to_date('2004-11 ...

  2. 08-图8 How Long Does It Take (25 分

    Given the relations of all the activities of a project, you are supposed to find the earliest comple ...

  3. linux shell 脚本 历史文件清理脚本,按天,按月,清理前N天的历史文件,删除指定大小历史文件,历史文件归档清理

    不知道大家那有没有要清理的这个事情.需要清理目录历史文件.可能后续也会有很多其他地方需要清理历史文件,可能会用到. 我这两天空闲写了个脚本,清理比较方便,有要进行清理的大量历史文件的话可以用. 脚本用 ...

  4. HDU 4622 Reincarnation Hash解法详解

    今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...

  5. JAVA多线程之线程池的使用

    合理利用线程池能够带来三个好处. 第一:降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗. 第二:提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行. 第三:提高线程 ...

  6. Java操作Excel之POI简单例子

    /** * 利用POI操作Excel表单 * * 需要jar包: * HSSF针对03及以前版本,即.xls后缀 * |---poi-3.16.jar * XSSF针对07及以后版本,即xlsx后缀 ...

  7. phpcms如何调用某一组图里的所有图片

    {pc:get sql="select * from v9_picture_data where id = '$id'"} {loop $data $n $r} {loop str ...

  8. <s:property>的用法

    1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法):  ...

  9. Every ending is just a new beginning.

    Every ending is just a new beginning.每次结束都是新的开始.

  10. TED:如何掌控你的自由时间以及让自己变得更好,这样就能看到爱情应有的样子

    TED:如何掌控你的自由时间以及让自己变得更好,这样就能看到爱情应有的样子 一.<如何掌控你的自由时间> (1)时间管理的传统思维:守时和节省零散的时间.演讲者认为这个观点已经彻底落后. ...