温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java流操作之数据流的示例分析

发布时间:2021-07-21 13:59:46 来源:亿速云 阅读:126 作者:小新 栏目:编程语言

这篇文章主要介绍Java流操作之数据流的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

 实例1:

package dataInputStreamAndPrintStreamDemo; 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.PrintStream; 
//示范如何自键盘读入字符串,并使用DataInputStream,PrintStream类将程序执行显示在屏幕(标准输出)上 
public class DataInputStreamAndPrintStreamDemo { 
 public static void main(String[] args) { 
 int count; 
 byte input[] = new byte[256]; 
 String InputString; 
 // 键盘读入 
 DataInputStream stdin = new DataInputStream(System.in); 
 //提高执行效率,几乎所有的InputStream类都可以被BufferedStream类包覆(wrap)来提高I/O效率 
 BufferedInputStream bufin = new BufferedInputStream(stdin); 
 // 屏幕输出 
 DataOutputStream stdout = new DataOutputStream(System.out);// 将结果输出至屏幕 
 BufferedOutputStream bufout = new BufferedOutputStream(stdout);// 提高输出效率 
 PrintStream p = new PrintStream(System.out);// 将结果输出至屏幕 
 try { 
 if (bufin.markSupported()) { 
 p.println("支持串流标记:是");// 使用PrintStream输出 
 p.println("输入字符串,结束请按【Enter】...\n" + "=>"); 
 //使得流在第一个位被作上标记(mark),并且会保留256位(mark(256)) 
 bufin.mark(256); 
 //读取字节并存放在指定的数组中 
 count = bufin.read(input); 
 p.println("读入字符数:" + count); 
 p.print("你输入的字符串为:"); 
 // 写入流,只是将数据写入流中而已,并不输出数据 
 // 所以在其后必须使用flush()函数将流中的数据强制输出 
 bufout.write(input, 0, count); 
 bufout.flush();// 强制输出至指定的输出装置 
 bufin.reset();// 将读取位置移至标记处,也就是流中的第一位 
 bufin.read(input, 0, count); 
 p.print("字符串的前半段:"); 
 bufout.write(input, 0, count / 2); 
 //相当于System.out.println(); 
 bufout.write((int)('\n')); 
 bufout.flush(); 
 bufin.reset(); 
 bufin.skip(count / 2); 
 bufin.read(input, 0, count / 2); 
 p.print("字符串的后半段:"); 
 bufout.write(input, 0, count / 2); 
 bufout.flush(); 
 } else { 
 System.out.println("字符串流标记:否"); 
 } 
 // 关闭流 
 p.close(); 
 stdin.close(); 
 bufin.close(); 
 stdout.close(); 
 bufout.close(); 
 } catch (IOException E) { 
 System.out.println("发生I/O错误!!!"); 
 } 
 } 
} 
//其实我们对PrintStream类应该很熟悉才对,System.out就是一个PrintStream类对象,其提供的print()和println()函数 
//几乎可显示所有数据类型的变量
//例程2:package iotest; 
 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.PrintStream; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
 
 
public class IOtest { 
 
 public static void main(String[] args) throws IOException { 
   
  byte buf[] = new byte[255]; 
  byte bufin[] = new byte[255];    //只能用byte格式将数据送入文件 
  String str = "输入的文字:"; 
  buf = str.getBytes(); 
  try { 
   FileOutputStream fout = new FileOutputStream("test.txt"); 
   PrintStream p = new PrintStream(fout); 
   p.println("输入的文字~~~~~~~"+'\n');  //方式一 
    fout.write(buf, 0, buf.length); //方式二 
    fout.write(buf);     //方式三 
    //fout.flush(); 
    //fout.close(); 
    System.out.println("快输入文字:"); 
    int bytes = System.in.read(bufin, 0, 255); 
        
    //追加文本!!!!!!!!!!!!!!!! 
    //fout = new FileOutputStream("test.txt",true); 
    fout.write(bufin, 0, bytes); 
  } catch (FileNotFoundException ex) { 
   Logger.getLogger(IOtest.class.getName()).log(Level.SEVERE, null, ex); 
  } 
   
 } 
  
}

结果:

//输入的文字~~~~~~~ 
 
 
//输入的文字:输入的文字:鍩庡競宸ヤ笟 fdsfdssssssssssssssssssssssssssss

以上是“Java流操作之数据流的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI