[转]java将字符串写入文件中

时间:2014-05-02 06:13:08   收藏:0   阅读:477
Java代码  bubuko.com,布布扣
    1. import java.io.File;  
    2. import java.io.FileNotFoundException;  
    3. import java.io.FileOutputStream;  
    4. import java.io.FileWriter;  
    5. import java.io.IOException;  
    6. import java.io.PrintStream;  
    7. import java.io.PrintWriter;  
    8. import java.io.RandomAccessFile;  
    9.   
    10. public class WriteStringToTxt {  
    11.   
    12.     public void WriteStringToFile(String filePath) {  
    13.         try {  
    14.             File file = new File(filePath);  
    15.             PrintStream ps = new PrintStream(new FileOutputStream(file));  
    16.             ps.println("http://www.docin.com/p-315288370.html");// 往文件里写入字符串  
    17.             ps.append("http://www.docin.com/p-315288370.html");// 在已有的基础上添加字符串  
    18.         } catch (FileNotFoundException e) {  
    19.             // TODO Auto-generated catch block  
    20.             e.printStackTrace();  
    21.         }  
    22.     }  
    23.   
    24.     public void WriteStringToFile2(String filePath) {  
    25.         try {  
    26.             FileWriter fw = new FileWriter(filePath, true);  
    27.             BufferedWriter bw = new BufferedWriter(fw);  
    28.             bw.append("在已有的基础上添加字符串");  
    29.             bw.write("abc\r\n ");// 往已有的文件上添加字符串  
    30.             bw.write("def\r\n ");  
    31.             bw.write("hijk ");  
    32.             bw.close();  
    33.             fw.close();  
    34.         } catch (Exception e) {  
    35.             // TODO Auto-generated catch block  
    36.             e.printStackTrace();  
    37.         }  
    38.     }  
    39.   
    40.     public void WriteStringToFile3(String filePath) {  
    41.         try {  
    42.             PrintWriter pw = new PrintWriter(new FileWriter(filePath));  
    43.             pw.println("abc ");  
    44.             pw.println("def ");  
    45.             pw.println("hef ");  
    46.             pw.close();  
    47.         } catch (IOException e) {  
    48.             // TODO Auto-generated catch block  
    49.             e.printStackTrace();  
    50.         }  
    51.     }  
    52.   
    53.     public void WriteStringToFile4(String filePath) {  
    54.         try {  
    55.             RandomAccessFile rf = new RandomAccessFile(filePath, "rw");  
    56.             rf.writeBytes("op\r\n");  
    57.             rf.writeBytes("app\r\n");  
    58.             rf.writeBytes("hijklllll");  
    59.             rf.close();  
    60.         } catch (IOException e) {  
    61.             e.printStackTrace();  
    62.         }  
    63.     }  
    64.   
    65.     public void WriteStringToFile5(String filePath) {  
    66.         try {  
    67.             FileOutputStream fos = new FileOutputStream(filePath);  
    68.             String s = "http://www.docin.com/p-315288370.html";  
    69.             fos.write(s.getBytes());  
    70.             fos.close();  
    71.         } catch (Exception e) {  
    72.             // TODO Auto-generated catch block  
    73.             e.printStackTrace();  
    74.         }  
    75.     }  
    76.   
    77.     public static void main(String[] args) {  
    78.         String filePath = "E:\\link.txt";  
    79.         // new WriteStringToTxt().WriteStringToFile(filePath);  
    80.         // new WriteStringToTxt().WriteStringToFile2(filePath);  
    81.         // new WriteStringToTxt().WriteStringToFile3(filePath);  
    82.         // new WriteStringToTxt().WriteStringToFile4(filePath);  
    83.         new WriteStringToTxt().WriteStringToFile5(filePath);  
    84.     }  
    85. }

[转]java将字符串写入文件中,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!