jsp生成和读取文件

来源:互联网 发布:d在c语言中是什么意思 编辑:程序博客网 时间:2024/06/02 06:47
package news;


import java.io.*;  
public class Test11 {  
    private static BufferedReader br = null;  
    private static File file = null;  
    private static BufferedWriter bw = null;  
    public static void main(String[] args) {  
        String b = null;  
        
        
        //file = new File("D:\\abc.txt");  
        
        file = new File("C:\\Users\\ye\\Desktop\\news\\bb.txt");
       
        System.out.println(file.getAbsolutePath());
        //System.out.println(file.getPath());
        String  path = file.getAbsolutePath();
      String[] sb = path.split("\\\\");
      
      for (String string : sb) {
    System.out.println(string);
}
        
        if (!file.exists() != false) {  
            try {  
                file.createNewFile();  
                  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
          }  
         try {  
            bw = new BufferedWriter(new FileWriter(file));  
            FileReader fr = new FileReader("C:\\Users\\ye\\Desktop\\news\\aa.txt");  
          
            br = new BufferedReader(fr);  
            while ((b = br.readLine()) != null) {  
            b= new String(b.getBytes("utf-8"),"utf-8");
                System.out.println(b);  
                bw.write(b);//输出字符串  
                bw.newLine();//换行  
                bw.flush();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }finally {  
            try {  
                br.close();  
                //bw.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
  
  
}  
0 0
原创粉丝点击