文章目录
Java 流(Stream)、文件(File)和IO
(一):控制台操作
1.读取控制台输入
Java 的控制台输入由System.in完成
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//把System.in 包装在BufferedReader的对象中来创建字符流
String str = br.readLine();//读取一个字符串
char c = (char) br.read();//读取一个字符
2.从控制台读取多字符输入
从 BufferedReader 对象读取一个字符要使用 read() 方法,它的语法如下:
int read( ) throws IOException
每次调用 read() 方法,它从输入流读取一个字符并把该字符作为整数值返回。 当流结束的时候返回 -1。该方法抛出 IOException。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char c;
System.out.println("输入字符,按q结束");
do{
c= (char) br.read();
System.out.println(c);
}while (c !='q');
3.从控制台读取字符串
使用BufferedReader中的readLine()方法
public static void main(String[] args) throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("请输入字符串,输入quit结束");
do{
str = br.readLine();
System.out.println(str);
}while (!str.equals("quit"));
}
(二)文件操作
1.File类
获取文件信息
- boolean exists():判断文件或目录是否存在
- String getName():返回文件或目录的名称
- String getParent():返回父路径的路径名字符串
- String getAbsolutePath():返回绝对路径的路径名字符串
- String getPath():返回抽象路径的路径名字符串
- boolean isAbsolute():判断当前路径是否为绝对路径
- boolean isDirectory():判断当前文件是否为目录
- boolean isFile():判断当前文件是否为一个标准文件
- boolean isHidden():判断当前文件是否是一个隐藏文件
- long lastModified():返回当前文件最后一次被修改的时间
- long length():返回当前文件的长度
- boolean canRead():判断是否可读
- boolean canWrite():判断是否可写
文件和文件夹的操作
- createNewFile():创建文件
- delete(): 删除文件
- renameTo():移动文件
- mkdir()和mkdirs():创建文件夹
- delete(): 删除文件夹
2.基础类:InputStream和OutputStream(字节流操作)
- FileInputStream/FileOutputStream(读写文件)
- BufferedInputStream/BufferedOutputStream(读写缓冲流)
- ByteArrayInputStream/ByteArrayOutputStream(按字节数组读写内存中临时数据)
- DataInputStream/DataOutputStream(读写基本类型和String)
- ObjectInputStream/ObjectOutputStream(读写对象)
- PipedInputStream/PipedOutputStream(主要在线程中使用)
- PrintStream(打印流,可以一次写一行)
3.基础类Reader和Writer(字符流操作)
- FileReader/FileWriter(只能采用系统默认编码方式读写)
- InputStreamReader/OutputStreamWriter(转换流,采用指定编码方式读写)
- BufferedReader/BufferedWriter(缓冲流,借助readLine()和newLine()可以一次读写一行)
- CharArrayReader/CharArrayWriter(按字符数组读写)
- PipedReader/PipedWriter(主要在线程中使用)
- PrintWriter(打印流,可以一次写一行)
4.代码实例:
输出文件相关属性
public static void main(String[] args) {
File file = new File("D:\\JAVA\\test.txt");//创建对象
if (file.exists()){//判断文件是否存在
System.out.println(("It's exists"));
System.out.println("last Modified is in: "+new Date(file.lastModified()));//输出修改时间
System.out.println("The length of this file is: "+ file.length());//输出文件大小
}else {
System.out.println(("It's not exists"));
}
//It's exists
//last Modified is in: Sat Apr 06 21:49:54 CST 2019
//The length of this file is: 20
}
读取文件中的字符
public static void main(String[] args) {
FileReader file = null;
int c= 0;
try {
file =new FileReader("D:\\JAVA\\test.txt");
while ((c=file.read())!= -1){
System.out.print((char) c);
}
file.close();
}
catch (FileNotFoundException e){
System.out.println("文件未找到");
}catch (IOException e){
System.out.println(("文件读取异常"));
}
}
//Hello MotherFucker
向一文中写入内容
public static void main(String[] args) {
int b = 0;
try {
File in = new File("D:\\JAVA\\test02.txt");//创建提供内容的文件的对象
File out = new File("D:\\JAVA\\test.txt");//创建接受内容的文件的对象
BufferedReader fr = new BufferedReader(new FileReader(in));//把读取的数据给fr
BufferedWriter fw = new BufferedWriter(new FileWriter(out,true));//内容添加到末尾
String line = fr.readLine();//将读取的内容给line
fw.write(line);//写入到fw中
fw.flush();//清除缓存
fw.close();//关闭输writer流
fr.close();//关闭输read流
}catch (FileNotFoundException e){
System.out.println(("文件未找到"));
}catch (IOException e){
System.out.println(("文件写入错误"));
}
System.out.println(("文件操作完成"));
多线程处理
(一)实例练习
1.创建进程:
- 通过实现 Runnable 接口来创建线程
class RunnableDemo implements Runnable{
private Thread t;
private String threadName;
public RunnableDemo(String threadName) {
this.threadName = threadName;
System.out.println("creating "+threadName);
}
@Override
public void run() {
System.out.println("running "+threadName);
try{
for (int i =4; i>0;i--){
System.out.println("Thread: " + threadName + ", " + i);
Thread.sleep(50);//每50ms输出一次
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
void start(){
System.out.println("Starting: "+threadName);
if (t == null){
t = new Thread(this,threadName);
t.start();
}
}
}
public class Test04 {
public static void main(String[] args) {
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
}
}
/*
creating Thread-1
Starting: Thread-1
creating Thread-2
Starting: Thread-2
running Thread-1
Thread: Thread-1, 4
running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Process finished with exit code 0
*/