import java.io.;每个代上方一定要import这个包。io包中包括各种类库与,代表某种输入或输出。*
1).最基本的类 InputStream类
2).Reader类
1).最基本的类OutputStream类
2).Writer类
notice:以上四个类均为抽象类,需要子类实现完成。
1).System类的所有属性和方法均为static的,也就是我们可以直接调用System类中的方法,比如System.out.print()等
2).System.out: out为static的,是PrintStream类的对象,用于输出字节数据流,对应标准输出设备屏幕。
System.in: in为static的,是InputStream类的对象,用于输入字节数据流,对应键盘
System.err: 静态,是PrintStream类的对象,对应系统错误信息的输出,对应屏幕。
1.输入字符
public static void main(String[] args) throws IOException{
char c;
c = (char)System.in.read();
}
1).System.in.read()的功能是从键盘输入中获取一个一个字节,然后通过(char)运算符转换为字符变量c。
2).使用read()方法时,要注意捕获和抛出IOException类异常。
2.输入字符串
package practice;
import java.io.*;
public class Standard_input {
public static void main(String[] args) throws IOException{
InputStreamReader iin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(iin);
String s;
float f;
int i;
boolean b;
System.out.println("Please input string");
s = bin.readLine();
System.out.println("Please input a flat number");
f = Float.parseFloat(bin.readLine());
System.out.println("Please enter the INteger");
i = Integer.parseInt(bin.readLine());
System.out.println(s);
System.out.println(f);
System.out.printn(i);
}
}
1).输入字符串时,需要调用BufferedReader类中的readline方法, 但是此类的构造方法中包含一个InputStreamReader的参数,因此要先创建一个InputStreamReader对象。
2).使用readline()方法接受到字符串后,便可以用Integer.parseInt()或者Float.parseFloat等方法来转换为我们所需的东西。
文件管理中有很多方法。
例程:
package practice;
import java.io.*;
public class File_manager {
public static long file_length;
public static void main(String[] args) {
String path = "d:\\1";
File file1 = new File(path, "2.txt");
File file2 = new File(path, "Second.jpg");
if(file1.exists()){
file_length = file1.length();
System.out.println("The length of the file is "+file_length);
if(file1.canWrite())
System.out.println("The file can be written");
else
System.out.println("The file cannot be written");
if(file1.canRead())
System.out.println("The file can be read");
else
System.out.println("The file cannot be read");
file1.renameTo(file2);
System.out.println(file1.getName());
}
}
}
使用的类:FileWriter, FileReader, BufferedReader, BufferedWrite.
输入:
例程:
package practice;
import java.io.*;
public class InputFile {
public static void main(String[] args) throws Exception{
InputStreamReader iin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(iin);//缓冲区输入来源来自于键盘
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\1\\2.txt"));//缓冲区输出位置为硬盘内存
String s;
while(true){
System.out.println("Please enter a string");
System.out.flush();
s = bin.readLine();
if(s.length() == 0)
break;
bw.write(s);
bw.newLine();
}
bw.close();
}
}
1).throws Exception因为可能有各种各样的异常,不需要一个个都写出来
2).System.out.flush()函数的功能是清空当前缓冲区
输出
例程:
package practice;
import java.io.*;
public class OutputFile {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("d:\\1\\dataFile.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("d:\\1\\targetFile.txt");
BufferedWriter bw = new BufferedWriter(fw);
int linenum = 0;
String s;
s = br.readLine();
while(s!=null){
bw.write(String.valueOf(linenum)+" : ");
linenum++;
bw.write(s);
bw.newLine();
s = br.readLine();
}
br.close();
bw.close();
}
}
1).String.valueOf()方法可以将基本类型转为字符串
最后一个程序,就相当于综合应用吧,将文件中的内容输出到显示屏上面
例程:
package practice;
import java.io.*;
public class OutputFile {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("d:\\1\\dataFile.txt");
BufferedReader br = new BufferedReader(fr);
OutputStreamWriter outr = new OutputStreamWriter(System.out);
BufferedWriter bw = new BufferedWriter(outr);
String s;
s = br.readLine();
while(s != null){
bw.write(s);
bw.newLine();
s = br.readLine();
}
br.close();
bw.close();
}
}
好啦,这次总结到此为止。这是我第一次写博客,希望在学习生活的课余时间,能坚持下去学习吧。