import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Io{
public static void main(String [] s){
File filename = new File("F:\\suncity.txt");
String filein="你好!";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename,"rw");
mm.writeBytes(filein);
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} finally{
if(mm!=null){
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成 catch 块
e2.printStackTrace();
}
}
}
}
}
这样写就可以了:
import java.io.*;
class C {
public static void main( String[ ] args ) throws Exception {
PrintWriter pw = new PrintWriter( new FileWriter( "问好.txt" ) );
pw.print( "你好" );
pw.close( );
}
}