java io流练习 写入 输入数据

String file_path = "D:\\can\\he.txt";
String file_directory = "D:\\can";
File file_create = new File(file_path);
File file_createdirectory = new File(file_directory);
FileOutputStream read_file = null;
if (file_create != null && !file_create.isFile()){
try {
file_create.createNewFile();
}catch (Exception e) {
e.printStackTrace();
}

try {
read_file = new FileOutputStream(file_path,true);
read_file.write(new String("hello wolrd").getBytes());
read_file.write(new String("hello wolrd").getBytes());
read_file.write(new String("hello wolrd").getBytes());
read_file.write(new String("hello wolrd").getBytes());
read_file.write(new String("hello wolrd").getBytes());

}catch (IOException e){
e.printStackTrace();
} finally{
try {
read_file.close();
}catch(Exception e){
e.printStackTrace();
}
}

byte[] read_buffered = new byte[2000];
FileInputStream read_file_memory = null;
int len = 0;
try{
read_file_memory = new FileInputStream(file_path);
while((len = read_file_memory.read(read_buffered)) != -1){
System.out.println(new String(read_buffered,0,read_buffered.length));
}
}catch(Exception e){
e.printStackTrace();
} finally{
try{
read_file_memory.close();
}catch (Exception e){
e.printStackTrace();
}
}


}
posted @ 2021-10-05 10:31  手摇点歌机  阅读(79)  评论(0)    收藏  举报