I\O接口练习

import java.io.*;

public class BufferedStreanTest2 {
public static void main(String[] args) throws IOException {
String content[]={"你不喜欢你,","我一点都不介意","因为我活下来,",
"不是为了取悦你."};
File file=new File("word1.txt");
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos=null;
BufferedOutputStream bos=null;
FileInputStream fis=null;
BufferedInputStream bis=null;
try{
fos=new FileOutputStream(file);
bos=new BufferedOutputStream(fos);
byte[] bContent=new byte[1024];
for(int k=0;k<content.length;k++){
bContent=content[k].getBytes();
bos.write(bContent);
}
System.out.println("写入成功!\n");
}catch (IOException e){
e.printStackTrace();
}finally{
try {
bos.close();
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
try{
fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
byte[] bContent=new byte[1024];
int len=bis.read(bContent);
System.out.println("文件中的信息是:"+new String(bContent,0,len));
}catch (IOException e){
e.printStackTrace();
}finally{
try{
fis.close();
bis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;

public class BufferedTest2 {
public static void main(String[] args) throws IOException {
String content[]={"你不喜欢我,","我一点都不介意","因为我活下来,","不是为了取悦你"};
File file=new File("yuzh,txt");
if(!file.exists()){
file.createNewFile();
System.out.println("文件创建成功");
}
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bufw = new BufferedWriter(fw);
for (int i = 0; i < content.length; i++) {
bufw.write(content[i]);
bufw.newLine();
}
bufw.close();
fw.close();
}catch (IOException e){
e.printStackTrace();
}
try{
FileReader fr=new FileReader(file);
BufferedReader bure=new BufferedReader(fr);
String s=null;
int i=0;
while((s=bure.readLine())!=null){
i++;
System.out.println("第"+i+"行:"+s);
}
bure.close();
fr.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ConsoleOutput2 {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("src/ConsokeOutput2.java");
char[] c=new char[1024];
int hasread=0;
while((hasread=reader.read(c))>0){
System.out.println(new String(c,0,hasread));
}
reader.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}


posted @ 2020-03-26 21:30  哈哈,呵呵,嘿嘿  阅读(266)  评论(0)    收藏  举报