一、读取控制台输入
Java的控制台输入有System.in完成。
为了获得一个绑定到控制台的字符流,你可以把 System.in
包装在一个 BufferedReader 对象中来创建一个字符流。
下面是创建 BufferedReader 的基本语法:
BufferReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader 对象创建后,我们便可以使用 read() 方法从控制台读取一个字符,或者用 readLine() 方法读取一个字符串。
实例:
package com.didi;
import java.io.*;
public class BufferedReaderTest {
public static void main(String[] args) throws IOException{
char c;
String str;
// 使用System.in创建BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// System.out.println("输入字符:按下'q'键退出:");
System.out.println("输入字符串(输入end结束):");
// 读取字符
do {
// c = (char)br.read(); // 读入一个字符
str = br.readLine(); // 读入字符串
System.out.println(str);
// } while (c != "q");
} while (!str.equals("end"));
}
}
二、读写文件(FileInputStream、FileOutputStream)
1、FileInputStream
该流用于从文件读取数据,它的对象可以用关键字 new 来创建。
有多种构造方法可用来创建对象。
可以使用字符串类型的文件名来创建一个输入流对象来读取文件:
InputStream f = new FileInputStream("C:/java/hello");
也可以使用一个文件对象来创建一个输入流对象来读取文件。我们首先得使用 File() 方法来创建一个文件对象:
File f = new File("C:/java/hello");
InputStream out = new FileInputStream(f);
创建了InputStream对象,就可以使用下面的方法来读取流或者进行其他的流操作。
序号 | 方法 | 描述 |
1 | public void close() throws IOException{} | 关闭文件输入流,释放资源 |
2 | protected void finalize() throws IOException{} | 清楚与该文件的链接 |
3 | public int read(int r) throws IOException{} | 从InputStream对象读取指定字节的数据,返回为整数值,返回下一个字节数据,如果已经到结尾则返回-1 |
4 | public int read(byte[] r) throws IOException{} | 从输入流读取r.length长度的字节。返回读取的字节数,如果是文件结尾,则返回-1 |
5 | public int available() throws IOException{} | 返回下一次对此输入流调用的方法,可以不接受阻塞地从此输入流读取的字节数。返回一个整数值。 |
2、FileOutputSteam
该类用来创建一个文件并向文件中写数据。
如果该流在打开文件进行输出前,目标文件不存在,那么该流会创建该文件。
有两个构造方法可以用来创建 FileOutputStream 对象。
使用字符串类型的文件名来创建一个输出流对象:
OutputStream f = new FileOutputStream("C:/java/hello")
也可以使用一个文件对象来创建一个输出流来写文件。我们首先得使用File()方法来创建一个文件对象:
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
创建OutputStream 对象完成后,就可以使用下面的方法来写入流或者进行其他的流操作。
序号 | 方法 | 描述 |
1 | public void close() throws IOException{} | 关闭文件输入流,释放资源 |
2 | protected void finalize() throws IOException{} | 清楚与该文件的链接 |
3 | public int write(int w) throws IOException{} | 将指定的字节写入输出流 |
4 | public int write(byte[] w) throws IOException{} | 将指定数组中w.length长度的字节写入输出流 |
实例:
package com.didi;
import java.io.*;
public class FileTest {
public static void main(String[] args) {
try {
String[] bWrite = {"hello", "world"};
File f = new File("test.txt");
FileOutputStream fop = new FileOutputStream(f);
OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
for (int x = 0; x < bWrite.length; x++) {
// System.out.println(bWrite[x]);
writer.write(bWrite[x]);
writer.append("\n");
}
writer.close(); // 关闭写入流
fop.close(); // 关闭输入流,释放系统资源
InputStream fin = new FileInputStream(f);
InputStreamReader reader = new InputStreamReader(fin, "UTF-8");
StringBuffer sb = new StringBuffer();
while(reader.ready()) {
sb.append((char) reader.read());
}
System.out.println(sb.toString());
reader.close();
fin.close();
} catch (IOException e) {
System.out.println("Exception");
}
}
}
完整示例代码地址:Day15