- 论坛徽章:
- 0
|
We all know write a string to a file is simple. Such as:
- import java.io.*;
- class FileWrite
- {
- public static void main(String args[])
- {
- try{
- // Create file
- FileWriter fstream = new FileWriter("out.txt");
- BufferedWriter out = new BufferedWriter(fstream);
- out.write("Hello Java");
- //Close the output stream
- out.close();
- }catch (Exception e){//Catch exception if any
- System.err.println("Error: " + e.getMessage());
- }
- }
- }
复制代码
I have a case which needs write strings in many methods.
How can I pass file name or stream and implement it?
(Open the stream, pass the stream into each method)
- public void doDiff(String oldFile, String newFile, String Directory) {
-
- String path = System.getProperty("user.dir");
- String DiffFileName = path+'/'+Directory+'/'+ oldFile+"D.txt";
-
- File file = new File(Directory,DiffFileName);
- try{
- FileWriter outFile = new FileWriter(file);
- BufferedWriter out = new BufferedWriter(outFile);
- String s1 = ">>>> Difference of file \"" + oldFile
- + "\" and file \"" + newFile + "\".\n";
- out.write(s1);
- out.flush();
- }
- catch (Exception e){//Catch exception if any
- System.err.println("Error: " + e.getMessage());
- }
- showinsert(file)
- }
- void showinsert(File file)
- {
- if ( printstatus == change ) println( ">>>> CHANGED TO" );
- else if ( printstatus != insert )
- String sn= ">>>> INSERT BEFORE " + printoldline ;
- // how can I print sn into file
- // do I need to create stream again?
- printstatus = insert;
- newinfo.symbol[ printnewline ].showSymbol();
- anyprinted = true;
- printnewline++;
- }
复制代码 |
|