------- android培训、java培训、期待与您交流! ----------
按流向:输入流、输出流;
按数据:字节流,字符流。
基类:
字节流:InputStream;OutPutStream
字符流:Reader;Writer;
该用哪一个流对象?
1、明确源和目的;
源:输入流 InputStream Reader
目的:输出流 OutputStream Writer
2、明确操作的数据是不是纯文本。
纯文本:字符流 Reader Writer
非纯文本:字节流 InputStream OutputStream
3、体系明确后,在确定使用那个具体的对象
通过设备区分:
源设备:内存,硬盘,键盘
目的设备:内存,硬盘,控制台
public class RemoveDirTest { public static void main(String[] args) { File dir = new File("e:\\demodir"); removeDir(dir); } public static void removeDir(File dir) { File[] files = dir.listFiles(); for(File file : files){ if(file.isDirectory()){ removeDir(file); }else{ System.out.println(file+":"+file.delete()); } } System.out.println(dir+":"+dir.delete()); } }
public class FileTest { public static void main(String[] args) { File dir = new File("d:\\java\\workspace"); listAll(dir,0); } public static void listAll(File dir,int level) { System.out.println(getSpace(level)+dir.getName()); //获取指定目录下当前的所有文件夹或者文件对象 level++; File[] files = dir.listFiles(new FileFilter(){ @Override public boolean accept(File pathname) { return pathname.isDirectory()||pathname.getName().endsWith(".java"); } }); /* FilenameFilter filter = new FilenameFilter(){ @Override public boolean accept(File dir, String name) { return name.endsWith(".java"); } }; filter.accept(dir,file.getName());//过滤的另一种方式 */ for(int x=0; x<files.length; x++){ if(files[x].isDirectory()){ listAll(files[x],level); } else System.out.println(getSpace(level)+files[x].getName()); } } private static String getSpace(int level) { StringBuilder sb = new StringBuilder(); for(int x=0; x<level; x++){ sb.append(" "); } sb.append("|--"); return sb.toString(); } }
public class PropertiesTest { public static void main(String[] args) throws IOException { getAppCount(); } public static void getAppCount() throws IOException{ //将配置文件封装成File对象。 File confile = new File("count.properties"); if(!confile.exists()){ confile.createNewFile(); } FileInputStream fis = new FileInputStream(confile); Properties prop = new Properties(); prop.load(fis); //从集合中通过键获取次数。 String value = prop.getProperty("time"); //定义计数器。记录获取到的次数。 int count =0; if(value!=null){ count = Integer.parseInt(value); if(count>=5){ throw new RuntimeException("使用次数已到,请注册,给钱!"); } } count++; //将改变后的次数重新存储到集合中。 prop.setProperty("time", count+""); FileOutputStream fos = new FileOutputStream(confile); prop.store(fos, ""); fos.close(); fis.close(); } }
print()方法可以将各种数据和数据类型原样打印。不会抛出IOException.
构造函数可以接受的数据类型,如果目标是输出流,可以加自动刷新标记
public class SequenceInputStreamDemo { public static void main(String[] args) throws IOException { //需求:将1.txt 2.txt 3.txt文件中的数据合并到一个文件中。 /* Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("1.txt")); v.add(new FileInputStream("2.txt")); v.add(new FileInputStream("3.txt")); Enumeration<FileInputStream> en = v.elements();//---Vector的elements()方法返回该集合的枚举 */ ArrayList<FileInputStream> al = new ArrayList<FileInputStream>(); for(int x=1; x<=3; x++){ al.add(new FileInputStream(x+".txt")); } Enumeration<FileInputStream> en = Collections.enumeration(al); //---使用集合工具类的enumeration(Collection col)方法返回枚举. /* final Iterator<FileInputStream> it = al.iterator(); //---局部内部类只能访问被final修饰的局部变量 Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){//---自定义枚举 @Override public boolean hasMoreElements() { return it.hasNext(); } @Override public FileInputStream nextElement() { return it.next(); } };*/ SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("1234.txt"); byte[] buf = new byte[1024]; int len = 0; while((len=sis.read(buf))!=-1){ fos.write(buf,0,len); } fos.close(); sis.close(); } }
public class MergeFile {//---合并dir目录下的文件 public static void main(String[] args) throws IOException { File dir = new File("c:\\partfiles"); mergeFile(dir); } public static void mergeFile(File dir) throws IOException { //---合并文件 //获取指定目录下的配置文件对象。 File[] files = dir.listFiles(new SuffixFilter(".properties")); if(files.length!=1) throw new RuntimeException(dir+",该目录下没有properties扩展名的文件或者不唯一"); File confile = files[0]; //获取文件中配置信息:合并文件名,文件个数================================== Properties prop = new Properties(); FileInputStream fis = new FileInputStream(confile); prop.load(fis); String filename = prop.getProperty("filename"); int count = Integer.parseInt(prop.getProperty("partcount")); //获取该目录下的所有碎片文件。 ============================================== File[] partFiles = dir.listFiles(new SuffixFilter(".part")); if(partFiles.length!=(count-1)){ throw new RuntimeException(" 碎片文件不符合要求,个数不对!应该"+count+"个"); } //将碎片文件和流对象关联 并存储到集合中。 ArrayList<FileInputStream> al = new ArrayList<FileInputStream>(); for(int x=0; x<partFiles.length; x++){ al.add(new FileInputStream(partFiles[x])); } //将多个流合并成一个序列流。 Enumeration<FileInputStream> en = Collections.enumeration(al); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream(new File(dir,filename)); byte[] buf = new byte[1024]; int len = 0; while((len=sis.read(buf))!=-1){ fos.write(buf,0,len); } fos.close(); sis.close(); } } public class SuffixFilter implements FilenameFilter { private String suffix; public SuffixFilter(String suffix) { super(); this.suffix = suffix; } @Override public boolean accept(File dir, String name) { return name.endsWith(suffix); } } public class SplitFileDemo {//---切割文件,并将信息保存到配置文件中 private static final int SIZE = 1024 * 1024; public static void main(String[] args) throws Exception { File file = new File("c:\\aa.mp3"); splitFile(file); } private static void splitFile(File file) throws IOException { // 用读取流关联源文件。 FileInputStream fis = new FileInputStream(file); // 定义一个1M的缓冲区。 byte[] buf = new byte[SIZE]; // 创建目的。 FileOutputStream fos = null; int len = 0; int count = 1; /* * 切割文件时,必须记录住被切割文件的名称,以及切割出来碎片文件的个数。 以方便于合并。 * 这个信息为了进行描述,使用键值对的方式。用到了properties对象 */ Properties prop = new Properties(); File dir = new File("c:\\partfiles"); if (!dir.exists()) dir.mkdirs(); while ((len = fis.read(buf)) != -1) { fos = new FileOutputStream(new File(dir, (count++) + ".part")); fos.write(buf, 0, len); fos.close(); } //将被切割文件的信息保存到prop集合中。 prop.setProperty("partcount", count+""); prop.setProperty("filename", file.getName()); fos = new FileOutputStream(new File(dir,count+".properties")); //将prop集合中的数据存储到文件中。 prop.store(fos, "save file info"); fos.close(); fis.close(); } }
public class PipedStream { public static void main(String[] args) throws IOException { PipedInputStream input = new PipedInputStream(); PipedOutputStream output = new PipedOutputStream(); input.connect(output); new Thread(new Input(input)).start(); new Thread(new Output(output)).start(); } } class Input implements Runnable{ private PipedInputStream in; Input(PipedInputStream in){ this.in = in; } public void run(){ try { byte[] buf = new byte[1024]; int len = in.read(buf); String s = new String(buf,0,len); System.out.println("s="+s); in.close(); } catch (Exception e) { } } } class Output implements Runnable{ private PipedOutputStream out; Output(PipedOutputStream out){ this.out = out; } public void run(){ try { Thread.sleep(5000); out.write("hi,管道来了!".getBytes()); } catch (Exception e) { } } }
public class Test { public static void main(String[] args) throws IOException { String str = "ab你好cd谢谢"; // int len = str.getBytes("gbk").length; // for(int x=0; x<len; x++){ // System.out.println("截取"+(x+1)+"个字节结果是:"+cutStringByByte(str, x+1)); // } int len = str.getBytes("utf-8").length; for(int x=0; x<len; x++){ System.out.println("截取"+(x+1)+"个字节结果是:"+cutStringByU8Byte(str, x+1)); } } public static String cutStringByU8Byte(String str, int len) throws IOException { byte[] buf = str.getBytes("utf-8"); int count = 0; for(int x=len-1; x>=0; x--){ //---判断截取的最后几位是什么字符。 if(buf[x]<0) count++; else break; } if(count%3==0) //---UTF-8的汉字是3个负数 return new String(buf,0,len,"utf-8"); else if(count%3==1) return new String(buf,0,len-1,"utf-8"); else return new String(buf,0,len-2,"utf-8"); } public static String cutStringByByte(String str,int len) throws IOException{ byte[] buf = str.getBytes("gbk"); int count = 0; for(int x=len-1; x>=0; x--){ if(buf[x]<0) count++; else break; } if(count%2==0) return new String(buf,0,len,"gbk"); else return new String(buf,0,len-1,"gbk"); } }