java学习笔记(二)流

来源:互联网 发布:php b2c商城系统 编辑:程序博客网 时间:2024/05/17 06:41
介绍System.out“标准”输出流。
定义:static PrintStream out 
 又因为 PrintStream是OutputStream的子类所以可以如下使用:
OutputStream ops = System.out

ops.write("wo shi haoren".getBytes());//向显示器输出

static PrintStream err 
System.err.print("我错了");//专门用来输出错误信息,在IDE中一般显示为红色 


static InputStream in   “标准”输入流
 接受键盘输入的三种方式:

InputStream in = System.in;//方式一:(只能接受个数限制的输入)/*byte[] b = new byte[1000];System.out.println("in:");int l = in.read(b);System.out.print(new String(b,0,l));in.close();*/InputStream in = System.in;//方式二:(不能接受汉字)/*StringBuffer sb = new StringBuffer();int temp =0;System.out.println("in:");while(((temp = in.read())!= -1)){char c = (char) temp;if(c == '\n'){break;}sb.append(c);}System.out.print(sb);in.close();*/InputStream in = System.in;//方式三:(标准输入)/*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.print("setIn:");String s = br.readLine();System.out.print(s);in.close();*/

输入输出的重定向
static void setErr(PrintStream err) 重新分配“标准”错误输出流。 
static void setIn(InputStream in) 重新分配“标准”输入流。 
static void setOut(PrintStream out) 重新分配“标准”输出流。 

输出重定向

String path = "D:"+File.separator+"text"+File.separator+"text7.text";System.setOut(new PrintStream(new FileOutputStream(path)));System.out.print("我来了!!!");


错误重定向

int a = 3;int b = 0;int c ;String path = "D:"+File.separator+"text"+File.separator+"text8.text";System.setOut(new PrintStream(new FileOutputStream(path)));try {c = a/b;} catch (Exception e) {System.out.print(e);}


重定向输入

String path = "D:"+File.separator+"text"+File.separator+"text9.text";System.setIn(new FileInputStream(path));InputStream is = System.in;byte[] b = new byte[1000];int l = is.read(b);System.out.println(new String(b,0,l));


介绍Scanner类

//Scanner scan = new Scanner(System.in);//从键盘接收数据Scanner scan = new Scanner(new FileInputStream("D:"+File.separator+"text"+File.separator+"text9.text"));//从文件输入//改变以空格做为分隔符,如果不改就只能输出空格之前的scan.useDelimiter("\n");System.out.print("setin:");String s = scan.next();System.out.print(s);
读写基本数据类型的流
DataOutputStream 和DataInputStream String path = "D:"+File.separator+"soft"+File.separator+"test10.text";File f = new File(path);DataOutputStream dops = new DataOutputStream(new FileOutputStream(f));int age = 2;int num = 5;double price = 43.5;dops.writeInt(age);dops.writeChar('\t');dops.writeDouble(price);dops.writeChar('\t');dops.writeInt(5);dops.close();DataInputStream dips = new DataInputStream(new FileInputStream(path));//读的顺序要和写的顺一致System.out.print(dips.readInt());System.out.print(dips.readChar());System.out.print(dips.readDouble());System.out.print(dips.readChar());System.out.print(dips.readInt());dips.close();
多行读取
String name1;int age1;double score1;char[] temp;char c;int len;for(int i=0;i<age.length;i++){temp = new char[1024];len =0;while((c=dips.readChar())!='\t'){temp[len] = c;len++;}name1 = new String(temp,0,len);age1 = dips.readInt();dips.readChar();score1 = dips.readDouble();dips.readChar();System.out.println(name1+" "+age1+" "+score1);


合并流
SequenceInputStream
合并两个文件
SequenceInputStream(InputStream s1, InputStream s2) FileInputStream is1 = new FileInputStream(f1); FileInputStream is2 = new FileInputStream(f2); FileOutputStream is3 = new FileOutputStream(f3);SequenceInputStream  s = new SequenceInputStream(is1, is2);int temp=0;while((temp=s.read())!=-1){is3.write(temp);}

合并多个文件
SequenceInputStream(Enumeration<? extends InputStream> e) 
可以利用集合工具类产生Enumeration对象
FileInputStream is1 = new FileInputStream(f1); FileInputStream is2 = new FileInputStream(f2); FileInputStream is4 = new FileInputStream(f4); FileOutputStream is3 = new FileOutputStream(f3);ArrayList<FileInputStream> ar = new ArrayList<FileInputStream>();ar.add(is1);ar.add(is2);ar.add(is4);Enumeration<FileInputStream> et = Collections.enumeration(ar);SequenceInputStream  s = new SequenceInputStream(et);
分割器
class UtilDemo{public static void split(File f) throws IOException{InputStream ips = new FileInputStream(f);byte[] b = new byte[1024*1024];OutputStream ops = null;int len = 0;int count = 1; File dir = new File("D:"+File.separator+"split");if(!dir.exists()){dir.mkdir();}while((len=ips.read(b))!=-1){ops = new FileOutputStream(new File(dir,(count++)+".p"));ops.write(b, 0, len);}ops.close();ips.close();}

合并
public static void merge() throws IOException{Collection<FileInputStream> coll = new ArrayList<FileInputStream>();File dir = new File("D:"+File.separator+"split");for(int i=0;i<5;i++){coll.add(new FileInputStream(new File(dir,(i+1)+".p")));}Enumeration<FileInputStream>  e = Collections.enumeration(coll);SequenceInputStream sq = new SequenceInputStream(e);//OutputStream ops = new FileOutputStream(new File(dir,"mz.jpg"));OutputStream ops = new FileOutputStream("D:"+File.separator+"mz.jpg");byte[] b = new byte[1024*1024];int len = 0;while((len = sq.read(b))!=-1){ops.write(b,0,len);}sq.close();ops.close();}

Properties集合类配置文件
Properties prs = new Properties();prs.setProperty("os-name", "windows");prs.setProperty("os-version", "win7");prs.setProperty("os-time", "2012-12-2");String path = "D:"+File.separator+"soft"+File.separator+"test27.text";File f = new File(path);OutputStream ips = new FileOutputStream(f);prs.store(ips, "key=values");ips.close();

//读出并修修改配置文件String path = "D:"+File.separator+"soft"+File.separator+"test27.text";File f = new File(path);InputStream ips = new FileInputStream(f);Properties prs = new Properties();prs.load(ips);从文件中读到Properties 集合中prs.setProperty("os-version", "win8");//修改OutputStream ops = new FileOutputStream(f);prs.store(ops, "sd");//prs.list(System.out);//读出的信息在控制台显示ips.close();


压缩流(ZipOutputStream)(了解)
//压缩文件String path1 = "D:"+File.separator+"soft"+File.separator+"test2.txt";String path2 = "D:"+File.separator+"soft"+File.separator+"te.zip";File f = new File(path1);InputStream ips = new FileInputStream(f);ZipOutputStream zops = new ZipOutputStream(new FileOutputStream(path2));zops.putNextEntry(new ZipEntry(f.getName()));//为每一个被压缩的文件设置名字zops.setComment("asdfasfasdfasdfasdfads");//设置注释int temp;while((temp=ips.read())!=-1){zops.write(temp);}ips.close();zops.close();


压缩文件夹(一定要关闭流不然就压缩不成功,文件名最好是用英文)String path3 = "D:"+File.separator+"qqq";String path4 = "D:"+File.separator+"dir.zip";File f1 = new File(path3);File fdir = new File(path4);ZipOutputStream zdir = new ZipOutputStream(new FileOutputStream(fdir));InputStream ips = null;if(f1.isDirectory()){File[] fa = f1.listFiles();for(int i=0;i<fa.length;i++){ips = new FileInputStream(fa[i]);zdir.putNextEntry(new ZipEntry(fa[i].getName()));int temp;while((temp=ips.read())!=-1){zdir.write(temp);}ips.close();}}zdir.close();

解压
while((ze = zips.getNextEntry()) != null){System.out.println("正在解压"+ze.getName());out = new File("d:"+File.separator+ze.getName());if(!out.getParentFile().exists()){out.getParentFile().mkdir();}if(!out.exists()){out.createNewFile();}ipsze = zp.getInputStream(ze);ops = new FileOutputStream(out);int temp;while((temp=ipsze.read())!=-1){ops.write(temp);}ops.close();ipsze.close();











0 0
原创粉丝点击