一、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. 转载 jQuery 整理的很详细,基本都在里面

    jQuery 函数     CSS 函数 $(a).css(name)     获取name属性值 $(a). css(name,value)      设置name的属性值 $(a).css({}) ...

  2. lintcode 解码方法

    简单的动态规划 class Solution { public: /* * @param s: a string, encoded message * @return: an integer, the ...

  3. ResourceBundle 读取properties文件中文乱码

    1.确认properties文件是什么编码格式,并确认文件在该格式下中文是正常显示的2.读取时候,进行转一层,先用ISO-8859-1读取字节流,然后根据properties的文件格式进行new St ...

  4. .db文件打开方式

    有时在工作中,数据库格式db后缀的格式,直接是打不开的,所以我这里使用了数据库管理工具,步骤如下 1. 在电脑安装 Navicat Premium,安装后在桌面生成图标,点击图标打开程序. 2.打开程 ...

  5. bootstrap栅格系统的实现

    bootstrap提供了一个非常实用的栅格系统,可以实现响应式的网格布局,原理其实很简单,利用了float.百分比的宽度和@media的配合实现响应式,bootstrap默认把一行分为了12列,提供了 ...

  6. sourceInsight4 破解笔记(完美破解)

    https://www.cnblogs.com/Napoleon-Wang/p/6706773.html 时隔好多年,sourceinsight4以迅雷不及掩耳之势的来了.与3.5相比,sourcei ...

  7. android 开发-系统设置界面的实现

    具体与Preference的用法类似,这里就不做过多解释,直接贴示例代码,需要在res下新建xml文件夹,在xml文件夹下添加xml文件. xml:(注意:root节点是:PreferenceScre ...

  8. SpringBoot | 第四章:日志配置(转)

    前言 介于平时工作中,对于日志这块没有过多的接触,也就未有过多的了解.故在编写本文时,上官网查看了相关资料,奈何每个字母我都认识,但合起来就有点晕了,英文阅读水平还是有待大大的提高呀.最后觉得还是转载 ...

  9. Linux Mint下的conky配置

    最近闲来无事,想把自己的Linux Mint弄的再炫酷点,在桌面上显示一些信息,因为我已经装了Cairo-dock,现在就差这个了,下面简单说下整个流程,首先你得安装conky, sudo apt-g ...

  10. 网页title旁边的小图片

    网页title旁边的小图片设置,图片格式必须是.ico <link rel="icon" href="img/logo.ico" type="i ...