public class FileIO {
/**
* 路径分隔符
*/
public static void test1() {
System.out.println(File.pathSeparator);
System.out.println(File.separator);
}
/**
* 创建文件
*/
public static void test2() {
File file = new File("readme.txt");//创建名为readme的txt文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字节流键盘输入
*/
public static void test3() throws IOException {
InputStream is = System.in;
int a;
try {
while ((a=is.read())!=-1) {
char b =(char) a;
System.out.print(b);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 字符输入流:BufferedReader(自带缓冲区)
*/
public static void test4() {
BufferedReader br= null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
while (br.ready()) {
System.out.println(br.readLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 字符输入流:InputStreamReader
*/
public static void test5() {
InputStreamReader is = null;
try {
is = new InputStreamReader(new FileInputStream("a.txt"));
int a;
while((a = is.read())!=-1)
System.out.print((char)a);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 字节输入流:FileInputStream
*/
public static void test6() {
InputStream is = null;
try {
is = new FileInputStream("a.txt");
int a;
while ((a = is.read())!=-1) {
char b =(char) a;
System.out.print(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件增加可执行权
*
*/
public static void test7() {
File file = new File("a.txt");
file.setExecutable(true);
System.out.println(file.canExecute());
}
/**
*文件重命名
*/
public static void test8() {
File file = new File("readme.txt");
file.renameTo(new File("b.txt"));
}
/**
* 查看文件权限
*/
public static void test9() {
File file = new File("a.txt");
System.out.println(file.canExecute()+","+file.canRead()+","+file.canWrite());
}
/**
* 文件权限操作
*/
public static void test10() {
File file = new File("a.txt");
file.setExecutable(true,false);
System.out.println(file.canExecute()+","+file.canRead()+","+file.canWrite());//查看执行权,读取权,写入权
}
/**
* 增强For
*/
public static void test11() {
String a = "a";
byte[] b = a.getBytes();
System.out.println(b.length);
for (byte i : b) {
System.out.println(i);
}
}
/**
* 功能:演示一下ByteArrayInputStream
* *将数组通过字节流ByteArrayInputStream读进内存
*/
public static void test12() {
byte[] b = "hello".getBytes();
InputStream is = new ByteArrayInputStream(b);
int a;
try {
while ((a = is.read()) != -1) {
char c = (char) a;
System.out.print(c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 字节输入流:BufferedInputStream(自带缓冲区)
*/
public static void test13() {
BufferedInputStream bs = null;
try {
bs = new BufferedInputStream(new FileInputStream("a.txt"));
byte[] byteArray = new byte[20];
try {
while ((bs.read(byteArray)) != -1) {
System.out.println(new String(byteArray));
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
bs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}