System.setOut(PrintStream out);Reassigns the "standard" output stream. First, if there is a security manager, its checkPermission method is called with a RuntimePermission("setIO") permission to see if it's ok to reassign the "standard" output stream. Parameters: out - the new standard output stream Throws: SecurityException - if a security manager exists and its checkPermission method doesn't allow reassigning of the standard output stream. Since: JDK1.1
File file=new File("D:/test.txt");//打开文件
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);//创建文件流
for(int i=0;i<10;i++){
StringBuffer sb=new StringBuffer();
sb.append("Hello");//在该文件末尾追加
}
out.close();//关闭文件
}
楼主,给分
File file=new File("D:/test.txt");//打开文件
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);//创建文件流
for(int i=0;i<10;i++){
StringBuffer sb=new StringBuffer();
sb.append("Hello");//在该文件末尾追加
}
out.close();//关闭文件
}
楼主,给分
First, if there is a security manager, its checkPermission method is called with a RuntimePermission("setIO") permission to see if it's ok to reassign the "standard" output stream.
Parameters:
out - the new standard output stream
Throws:
SecurityException - if a security manager exists and its checkPermission method doesn't allow reassigning of the standard output stream.
Since:
JDK1.1
File file=new File("D:/test.txt");//打开文件
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);//创建文件流
for(int i=0;i<10;i++){
StringBuffer sb=new StringBuffer();
sb.append("Hello");//在该文件末尾追加
out.write(sb.toString().getBytes("utf-8"));
}
out.close();//关闭文件
}
han!
try //重定向輸出流
{
String path = System.getProperties().getProperty("user.dir") + "\\log\\" + System.currentTimeMillis() + "_log.txt";//可以改路徑,如path="C:\\log.txt";
try {
new File(path).createNewFile();
} catch (IOException e) {
e.printStackTrace();
};
PrintStream ps = new PrintStream(path);
System.setOut(ps);
System.setErr(ps);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
String tempFilePath="d:\\result.txt";
File temp= new File( tempFilePath );
temp.mkdirs();
File f=new File(tempFilePath,"faq.xml");
f.createNewFile();
FileWriter fw = null;
fw = new FileWriter(f);
fw.write("aaaa\n");
fw.write("bb\tb");
fw.close();
}
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;public final class LogWriter extends MyObject
{
/*-------Member Property--------------------------------*/
private File m_File = null;
private FileWriter m_FileWriter = null;
private BufferedWriter m_BufferedWriter = null;
private PrintWriter m_PrintWriter = null; private String m_strFileName = null;
private String m_strLogsDirectory = null;
private String m_strBackupDirectory= null; private LogWriter()
{
} public LogWriter(ENTITY entDebugSetting)
{
m_strFileName = entDebugSetting.getTextProperty("LOGFILENAME"); String TS = DateTimeHelper.getSecondTS(); m_strFileName = m_strFileName.replaceAll("#DATETIME#",TS);
m_strFileName = m_strFileName.replaceAll("#APPLICATION#",MyObject.JAVA_APPLICATION_NAME); m_strLogsDirectory = entDebugSetting.getTextProperty("LOGS");
m_strBackupDirectory = entDebugSetting.getTextProperty("LOGS_BACKUP"); if(createFile(m_strLogsDirectory+m_strFileName))
if(createFileWriter())
if(createBufferedWriter())
createPrintWriter(); if(m_PrintWriter!=null)
{
println("<?xml version='"
+entDebugSetting.getTextProperty("XMLVERSION")
+"' encoding='"
+entDebugSetting.getTextProperty("XMLENCODING")
+"'?>"); println("<MYLOG>");
println(this.dump());
println("<APPLICATION>"+MyObject.JAVA_APPLICATION_NAME+"</APPLICATION>"); println("<TIME>"+TS+"</TIME>"); println("<GENERATOR>gdut.cims.LogWriter</GENERATOR>");
println("<DESIGNER>[email protected]</DESIGNER>");
}
} private boolean createFile(String filename)
{
try
{
m_File = new File(filename); if(!m_File.exists())
return m_File.createNewFile(); return true;
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this,"createFile",e);
return false;
}
} private boolean createFileWriter()
{
try
{
m_FileWriter = new FileWriter(m_File);
return true;
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this,"createFileWriter",e);
return false;
}
} private boolean createBufferedWriter()
{
try
{
m_BufferedWriter = new BufferedWriter(m_FileWriter);
return true;
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this,"createBufferedWriter",e);
return false;
}
}
protected PrintWriter getPrintWriter()
{
return m_PrintWriter;
}
private boolean createPrintWriter()
{
try
{
m_PrintWriter = new PrintWriter(m_BufferedWriter,true);//autoflush
return true;
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this,"createPrintWriter",e);
return false;
}
}
protected void destructor()
{
super.MyObjectDestructor(); println("</MYLOG>"); try
{
if(m_PrintWriter!=null)
m_PrintWriter.close(); if(m_BufferedWriter!=null)
m_BufferedWriter.close(); if(m_FileWriter!=null)
m_FileWriter.close(); if(m_File!=null)
{
//Move File
File backup = new File(m_strBackupDirectory+m_strFileName); if(!backup.getParentFile().exists())
backup.getParentFile().mkdir(); m_File.renameTo(backup); m_File.delete(); backup = null;
}
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this,"destructor()",e);
} m_File = null;
m_FileWriter = null;
m_BufferedWriter = null;
m_PrintWriter = null;
} protected String dump()
{
return "<LogWriter>"
+"\n\t<FILENAME>"+m_strLogsDirectory+m_strFileName+"</FILENAME>"
+"\n</LogWriter>";
} public void println(String aLine)
{
if(m_PrintWriter!=null)
{
m_PrintWriter.println(aLine);
}
}
}
class testio{
public static void main(String[] args) throws Exception{
//读取输入流
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入你要保存的字符:");
//读取一个文本行
String str=br.readLine();
//创建一个新的File对象
File f=new File("testio.txt");
//创建一个新的文件testio.txt
f.createNewFile();
//打印刚才读取的字符
System.out.println(str);
//创建输出流
FileOutputStream fos=new FileOutputStream("testio.txt");
//写到文本
fos.write(str.getBytes());
//关闭输出流
fos.close();
}
}