JAVA输入输出流
java输入输出流实验报告总结

java输入输出流实验报告总结Java输入输出流是Java中非常重要的概念,也是Java程序设计的基础。
在Java 程序中,输入输出流用于处理与设备之间的数据传输,例如键盘、鼠标、文件等。
Java的输入输出流主要包含两种类型:字节流和字符流。
字节流以字节为最小读写单位,而字符流以字符为最小读写单位。
Java的输入流有很多种类型,例如:InputStream、ByteArrayInputStream、FileInputStream等。
这些输入流的共同特点是读取外部数据到程序中。
Java的输出流也有多种类型,例如:OutputStream、ByteArrayOutputStream、FileOutputStream等。
这些输出流的共同特点是将程序中的数据输出到外部。
下面将以实验报告的形式进行总结,详细介绍Java输入输出流的原理和使用方法。
实验一:字节流输入输出1. 实验目的:掌握字节流输入输出的基本使用方法。
2. 实验内容:编写一个程序,实现从文件中读取数据,然后将数据输出到另一个文件中。
3. 实验步骤:a. 创建一个输入流对象,用于读取原始文件的数据。
b. 创建一个输出流对象,用于将读取到的数据输出到目标文件中。
c. 循环读取输入流中的数据,直到读取到文件末尾。
d. 将读取到的数据写入输出流中。
e. 关闭输入流和输出流。
4. 实验结果:成功读取原始文件中的数据,并将数据输出到目标文件中。
实验二:字符流输入输出1. 实验目的:掌握字符流输入输出的基本使用方法。
2. 实验内容:编写一个程序,实现从键盘输入文字,然后将文字输出到屏幕上。
3. 实验步骤:a. 创建一个字符输入流对象,用于读取键盘输入的文字。
b. 创建一个字符输出流对象,用于将读取到的文字输出到屏幕上。
c. 循环读取输入流中的文字,直到读取到换行符。
d. 将读取到的文字写入输出流中。
e. 关闭输入流和输出流。
4. 实验结果:成功从键盘输入文字,并将文字输出到屏幕上。
Java的输入与输出流(实验报告)

成都大学实验报告实验项目名称Java的输入与输出流一、实验目的:1. 理解I/O流的概念,掌握其分类2. 掌握文本文件读写、二进制文件读写二、实验内容(包括源程序及相关说明):1. 分别使用FileWriter 和BufferedWriter 往文件中写入10万个随机数,比较用时。
源代码如下:(1)import java.io.*;public class Ex1_1 {public static void main(String[] args) throws IOException{long t=System.currentTimeMillis();FileWriter fw =new FileWriter("d:\\Ex1.txt");for(int i=1;i<=100000;i++){fw.write((int)(Math.random()*10000)+" \n");}fw.close();t=System.currentTimeMillis()-t;System.out.println("The elapsed: "+t);}}(2)import java.io.*;public class Ex1_1 {public static void main(String[] args) throws IOException{long t=System.currentTimeMillis();BufferedWriter fw=new BufferedWriter(new FileWriter("d:\\Ex1.txt"));for(int i=1;i<=100000;i++){fw.write((int)(Math.random()*10000)+"\n");}fw.close();t=System.currentTimeMillis()-t;System.out.println("The elapsed: "+t);}}2. 生成一个html文件,使其能显示2的幂次(0~9)的表格如下:代码如下:import java.io.*;public class Ex1_1 {public static void main(String[] args) throws IOException{BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\Ex2.html"));bw.write("<table border=1 align=center width=200 height=250>");bw.newLine();bw.write("<tr><td align=center>Power of 2<td align=center>Value</tr>");for(int i=0;i<=9;i++){bw.write("<tr><td align=center>"+i+"<td align=center>"+Math.pow(i, 2)+"</tr>");}bw.write("</table>");bw.newLine();bw.close();}}3. 在文本文件bigbook.txt中包含有很长篇幅的英语短文,编写程序要求统计文件的所有短文中包含英文字母“A”的个数,并显示统计的时间。
java 输入输出流实验

}
catch(Exception exp){}
clip.start();
voiceWord.setEnabled(true);
}
}6.实验指导
(1)BufferedReader对象调用readLine方法可读取文件中的一行内容。
catch(IOException exp){}
}
}
public void run()
{
voiceWord.setEnabled(false);
try{
if(clip!=null)
{
clip.close();
}
clip=AudioSystem.getClip();
File voiceFile=new File(showWord.getText().trim()+".wav");
add(pCenter,BorderLayout.CENTER);
menubar=new JMenuBar();
menu=new JMenu("帮助");
help=new JMenuIter("关于学单词");
help.addActionListener(this);
menu.add(help);
menubar.add(menu);
FileReader类是Reader的子类,该类创建的对象称为文件字符输入流。文件字符输入流按字符读取文件中的数据。FileReader流按顺序读取文件,只要不关闭流,每次调用读取方法时FileReader流就会依次读取文件中其余的内容,直到文件的末尾或流被关闭。
java中的输入输出语句

在Java中,你可以使用System.out来进行标准输出,使用System.in来进行标准输入。
以下是一些基本的输入输出语句示例:1. 标准输出(System.out):public class HelloWorld {public static void main(String[] args) {System.out.println("Hello, World!");}}上述示例中,System.out.println用于将文本输出到控制台。
2. 标准输入(System.in):import java.util.Scanner;public class UserInput {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("Enter your name: ");String name = scanner.nextLine();System.out.print("Enter your age: ");int age = scanner.nextInt();System.out.println("Hello, " + name + "! You are " + age + " years old.");}}上述示例中,Scanner类用于从控制台读取用户的输入。
scanner.nextLine()用于读取一行文本,而scanner.nextInt()用于读取整数。
3. 格式化输出(System.out.printf):public class FormatOutput {public static void main(String[] args) {String name = "John";int age = 25;System.out.printf("Hello, %s! You are %d years old.%n", name, age);}}上述示例中,System.out.printf允许你使用格式字符串输出,并在其中插入变量的值。
(JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectO。。。

(JAVA)从零开始之--对象输⼊输出流ObjectInputStream、ObjectO。
对象的输⼊输出流 : 主要的作⽤是⽤于写⼊对象信息与读取对象信息。
对象信息⼀旦写到⽂件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输⼊流: ObjectInputStream使⽤:对象的输出流将指定的对象写⼊到⽂件的过程,就是将对象序列化的过程,对象的输⼊流将指定序列化好的⽂件读出来的过程,就是对象反序列化的过程。
既然对象的输出流将对象写⼊到⽂件中称之为对象的序列化,那么可想⽽知对象所对应的class必须要实现Serializable接⼝。
(查看源码可得知:Serializable接⼝没有任何的⽅法,只是作为⼀个标识接⼝存在)。
1、将User类的对象序列化1class User implements Serializable{//必须实现Serializable接⼝2 String uid;3 String pwd;4public User(String _uid,String _pwd){5this.uid = _uid;6this.pwd = _pwd;7 }8 @Override9public String toString() {10return "账号:"+this.uid+" 密码:"+this.pwd;11 }12 }1314public class Demo1 {1516public static void main(String[] args) throws IOException {17//假设将对象信息写⼊到obj.txt⽂件中,事先已经在硬盘中建⽴了⼀个obj.txt⽂件18 File f = new File("F:\\obj.txt");19 writeObjec(f);20 System.out.println("OK");21 }2223//定义⽅法把对象的信息写到硬盘上------>对象的序列化。
输入输出流

输入输出流的相关简介系别:计算机技术与信息管理系班级:09软件技术姓名:徐燕学号:09207745目录一.Java输入输出流详解 (3)1.什么是流: (3)2、流的分类 (3)2.1字节流 (3)2.2.字符流 (4)2.3.对象流 (4)4.字节流与字符流的区别: (4)二.流的层次结构 (5)1.InputStream类 (6)2.OutputStream类 (7)3.FileInputStream类 (7)4.FileOutputStream类 (8)5.File类 (8)6.从一个流构造另一个流 (9)7.BufferInputStream和bufferOutputStream (10)8.printstream (11)三.字符流的读取和写入 (11)1.BufferInputStream和bufferOutputStream (14)2.printstream (15)四.java的i/o输入输出类怎么分出是输入流还是输出流? (15)五.java中什么时候用输入流,什么时候用输出流? (16)六.JA V A输出输入流函数注意事项 (16)1. 处理概述: (17)2. 字节流: (17)2.1输入流: (17)2.2输出流: (18)3、字符流: (18)3.1. 字符流处理概述 (18)3.2. 字符流处理 (19)3.3流处理中的其它方法: (20)七.总结 (21)一.Java输入输出流详解stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。
在Java的IO中,所有的stream(包括Inputstream和Out stream)都包括两种类型:1.什么是流:流是一个抽象的概念。
当Java程序需要从数据源读取数据时,会开启一个到数据源的流。
数据源可以是文件,内存或者网络等。
同样,当程序需要输出数据到目的地时也一样会开启一个流,数据目的地也可以是文件、内存或者网络等。
Java自学第10期——File类与IO流(输入输出流、处理流、转换流、缓冲流、Prope。。。

Java⾃学第10期——File类与IO流(输⼊输出流、处理流、转换流、缓冲流、Prope。
1、IO简介IO(输⼊输出)通过java.io包下的类和接⼝来⽀持,包下包括输⼊、输出两种IO流,每种输⼊输出流⼜可分为字符流和字节流两⼤类。
2、File类File类是io包下与平台⽆关的⽂件和⽬录,File能新建、删除、重命名⽂件和⽬录,不能访问⽂件本⾝,后者需要使⽤输⼊输⼊流。
2.1 构造⽅法File类的构造⽅法:File(File parent, String child) 参数:⽗路径,⼦路径根据 parent 抽象路径名和 child 路径名字符串创建⼀个新 File 实例。
File(String pathname)通过将给定路径名字符串转换为抽象路径名来创建⼀个新 File 实例。
File(String parent, String child)根据 parent 路径名字符串和 child 路径名字符串创建⼀个新 File 实例。
File(URI uri)通过将给定的 file: URI 转换为⼀个抽象路径名来创建⼀个新的 File 实例。
2.2 静态⽅法File类静态⽅法:static String pathSeparator()与系统有关的路径分隔符,为了⽅便,它被表⽰为⼀个字符串。
static char pathSeparatorChar()与系统有关的路径分隔符。
static String separator()与系统有关的默认名称分隔符,为了⽅便,它被表⽰为⼀个字符串。
static char separatorChar()与系统有关的默认名称分隔符。
2.3 常⽤⽅法:2.3.1 获取相关public String getAbsolutePath()返回此File的绝对路径名字符串。
public String getPath()将此File转换为路径名字符串。
public String getName()返回由此File表⽰的⽂件或⽬录的名称。
java 标准输入输出流

java 标准输入输出流
计算机系统都有标准的输入设备和标准输出设备。
对一般系统而言,标准输入设备通常是键盘,而标准输出设备是屏幕。
Java程序经常需要从键盘上输入数据,从屏幕上输出数据,为此频繁创建输入/输出流对象将很不方便。
因此,Java语言事先定义好了两个对象,分别对系统的标准输入和标准输出相联系,如System.in和System.out,另外还定义了标准的System.err错误输出流。
System.in是标准输入流,是InputStream类的实例。
可以使用read()方法从键盘上读取字节,也可以将它包装成数据流读取各种类型的数据和字符串。
在使用System.in的read()方法时,由于read()方法在定义时抛出了IOException 异常,所以必须使用try-catch结构捕获异常或声明抛出异常;
System.out和System.err是标准输出流和标准错误输出流,是PrintStream类的实例。
如果从标准输入流键盘上输入基本数据类型,可以将标准输入流包装成具有缓
执行上述代码,结果如图12-12所示。
图12-12 System.in 标准输入流。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int read() int read(byte[] buffer) int read(byte[] buffer, int offset, int length)
其它方法:
void close() int available() skip(long n) boolean markSupported() void mark(int readlimit) void reset()
9.4常用输入输出流类(2)
FileReader-文件字符输入流
构造方法: public FileReader(String name) throws FileNotFoundException public FileReader(File file) throws FileNotFoundException
方法:参见InputStream FileOutputStream-文件字节输出流
构造方法: public FileOutputStream(String name) throws FileNotFoundException public FileOutputStream(File file) throws FileNotFoundException 方法:参见OutputStream
//标准输出流System.out是字节输出流
//把标准输出流转化成缓冲字节输出流 /用BufferedOutputStream中函数Write输出 //数据从缓冲区中全部输出
import java.io.*; public class Test3 { public static void main(String[] args) throws IOException{ String s = "Hello"; OutputStreamWriter o = new OutputStreamWriter(System.out); o.write(s); 把标准字节输出流转化 成字符输出流 o.flush(); } }
•
9.3 Writer类提供的方法
•
三个基本的write方法: – void write(int c) – void write(char[] cbuf) – void write(char[] cbuf, int offset, int length) – void write(String string) – void write(String string, int offset, int length) 其它方法 – void close() – void flush()
OutputStream 字节输出流
FilterOutputStream 过滤器输出流
BufferedOutputStream 缓冲输出流
ObjectOutputStream 对象输出流
DataOutputStream 数据输出流
ByteArrayOutputStream 字节数组输出流
9.2 InputStream类提供的方法
Reader
9.3 字符流(2)
字符输出流Writer类为所有面向字符的输出流的超类。
BufferedWriter CharArrayWriter StringWriter Writer OutputStreamWriter PrintWriter PipedWriter FilterWriter FileWriter
9.1 数据流基本概念(4)
java.io包中的数据流及文件类 包中的数据流及文件类 数据流类
InputStream OuputStream Reader Writer
说明
字节输入流的超类 字节输出流的超类 字符输入流的超类 字符输出流的超类
补充说明
它是抽象类,子类才能实例化 它是抽象类,子类才能实例化 它是抽象类,子类才能实例化 它是抽象类,子类才能实例化
方法:见InputStream BufferedOutputStream-缓冲输出字节流 构造方法:
BufferedOutputStream(OutputStream out):同样是为了提高效率. 方法:见OutputStream.
9.4常用输入\输出流类(4)
BufferedReader-字符缓冲输入流 构造方法: BufferedReader(Reader in):把一个普通的字符流转化成缓冲流,同样 可以提高输入效率. 方法:除了Reader中函数外,主要有: public String readLine(); //读一行文本 BufferedWriter-字符缓冲输出流 构造方法: BufferedWriter(Writer out) 方法:除了Writer中函数外,主要有: public void newLine(); //回车换行
•
9.4常用输入\输出流类
FileInputSream-文件字节输入流
构造方法: public FileInputStream(String name) throws FileNotFoundException public FileInputStream(File file) throws FileNotFoundException
第9讲 输入/输出流
9.1 数据流基本概念(1)
数据流(stream)是指一组有顺序的、有起点和终点的字 数据流 节集合,是对输入输出的总称(或抽象)。数据流完成 从键盘接收数据、读写文件以及打印等数据传输操作。
程序 键盘 显示器 输入流
打印机 输出流 文件 文件
图9.1 输入流与输出流
9.1 数据流基本概念(2)
9.3 Reader类提供的方法
•
三个基本的read方法: – int read() – int read(char[] cbuf) – int read(char[] cbuf, int offset, int length) 其它方法 – void close() – boolean ready() – skip(long n) – boolean markSupported() – void mark(int readAheadLimit) – void reset()
9.5示例1
以下四段代码均在屏幕上输出”Hello”
public class Test1 { public static void main(String []args) { System.out.println(“Hello”); import java.io.*; public class Test2 { public static void main(String []args) throws IOException { String s = "Hello"; BufferedOutputStream b = new BufferedOutputStream(System.out); b.write(s.getBytes()); b.flush(); } }
9.2 OutputStream类提供的方法
三个基本的write方法:
void write(int c) void write(byte[] buffer) void write(byte[] buffer, int offset, int length)
其它方法
void close() void flush()
import java.io.*; public class Test3 { public static void main(String[] args) throws IOException{ String s = "Hello"; OutputStreamWriter o = new OutputStreamWriter(System.out); BufferedWriter b = new BufferedWriter(o); b.write(s); b.flush(); } }
PushbackInputStream 回压输入流
InputStream 字节输入流
FilterInputStream 过滤器输入流
BufferedInputStream 缓冲输入流
ObjectInputStream 对象输入流
DataInputStream 数据输入流
SequenceInputStream 顺序输入流
方法:参见Reader FileWriter-文件字符输出流
构造方法: public FileWriter(String name) throws FileNotFoundException public FileWriter(File file) throws FileNotFoundException 方法:参见Writer
ByteArrayInputStream 字节数组输入流
StringBufferInputStream 缓冲字符串输入流
9.2 字节流(2)
字节输出流OutputStream类为所有面向字节的输出流的超类。
FileOutputStream 文件输出流
PipedOutputStream 管道输出流
PrintStream 格式化输出流
以下两个类是单独与文件相关的类,与上述流类不同 File RandomAccessFile 文件类 随机访问文件类 非抽象类 非抽象类
9.2 字节流
字节输入流InputStream类为所有面向字节的输入流的超类。
FileInputStream 文件输入流
PipedInputStream 管道输入流
9.1 数据流基本概念(3)
import java.io.*; public class Input1 { public static void main(String args[]) throws IOException { System.out.println("Input: "); byte buffer[] = new byte[512]; //输入缓冲区 输入缓冲区 int count = System.in.read(buffer); //读取标准输入流 读取标准输入流 System.out.println("Output: "); for (int i=0;i<count;i++) //输出 输出buffer元素值 输出 元素值 { System.out.print(" "+buffer[i]); } System.out.println(); for (int i=0;i<count;i++) //按字符方式输出 按字符方式输出buffer 按字符方式输出 { System.out.print((char) buffer[i]); } System.out.println("count = "+ count); //buffer实际长度 实际长度 } }