java文件/io等操作
package ljc.utils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Io {
public static void cmd() throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入a字符串: ");
String a = br.readLine();
System.out.println("请输入b整数: ");
int b = Integer.parseInt(br.readLine());
System.out.printf("%s %d\n", a, b);
Scanner args = new Scanner(System.in);
System.out.println("请输入一行参数: ");
String line = args.nextLine();
System.out.println("请输入整数: ");
int i1 = args.nextInt();
System.out.println("请输入浮点数: ");
Float d1 = args.nextFloat();
System.out.printf("%s %d %f\n",line,i1,d1);
}
public static void file_read() throws Exception {
InputStreamReader f = null;
try {
f = new InputStreamReader(new FileInputStream("./io.md"));
int temp;
while ((temp = f.read()) != -1) {
System.out.print((char) temp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (f != null) {
f.close();
}
}
System.out.println("\n读取完成");
}
public static void file_write() throws Exception {
OutputStreamWriter f = null;
try {
f = new OutputStreamWriter(new FileOutputStream("./test.txt"), "utf-8");
f.write("你好\n");
f.write("中国\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (f != null) {
f.close();
}
}
}
public static void test() throws Exception {
file_write();
}
}