请问如何创建文件? File f=new file("temp.txt");f.createNewFile(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 path="c:\\Tomcat";filename="forum";f=new File(path,filename+".txt"); 随便来一段吧!我以前写的/*创建新文件入口参数:文件路径: String path 文件名: String name 文件内容: String value返回参数:成功与否 : boolean*/ public boolean TextChanger(String path,String name,String value) { String filepath = path + "/" + name; String val = value; boolean bln = false; try { FileOutputStream out = null; out = new FileOutputStream(filepath); DataOutputStream textout = null; textout = new DataOutputStream(out); val = new String(val.getBytes("gb2312"),"8859_1") + "\r\n"; textout.writeBytes(val); textout.close(); out.close(); bln = true; } catch (Exception e) { } return bln; } 给你一个我们单位用的日志类package com.hoten.util;import java.io.*;import java.util.*;/** * * <p>Title: LOG 日志记录</p> * <p>Description: * 此类主要用来记录系统中发生的重大事件,以及由于程序本身所产生的错误信息</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: hoten </p> * @author lqf * @version 1.0 *//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public class Log{ /** * 用来记录系统重大事件 * @param msg 重大事件 * @param fileName 日志文件的路径及名称 */ public synchronized static void printEvent(String msg,String fileName) { msg = new String( "时间:"+Time.getTime(Time.YYMMDDhhmmss) + " 事件消息: " + msg); if(fileName!=null) printToFile(msg,fileName); else print(msg); return; }/** * 记录应程序本身发生的错误,主要给程序员观察。 * @param e 一个Exception * @param mobile 用户手机号码 * @param msg 用户发送的消息 * @param fileName 日志文件的路径及名称 */ public synchronized static void printError(Throwable e,String mobile,String msg,String fileName) { StringBuffer errors=new StringBuffer(100); errors.append("时间:"); errors.append(Time.getTime(Time.YYMMDDhhmmssxxx)); errors.append(" 手机号码:"); errors.append(mobile); errors.append(" 消息:"); errors.append(msg); errors.append(" Exception: "); if(fileName!=null) { printToFile(errors.toString().trim(),fileName); try { e.printStackTrace(new PrintWriter(new FileWriter(fileName,true),true));// } catch (Exception ex) { } } else print(errors.toString().trim()); return; }/**把错误消息打印到屏幕上 * * @param msg 错误消息 */ private static void print(String msg) { System.out.println(msg); }/** * 把消息打印到指定文件 * @param msg 错误消息 * @param fileName 指定的文件 */ private static void printToFile(String msg,String fileName) //打印到文件中 { BufferedWriter mBufWriter = null; try { if(!createFile(fileName)) return ; FileWriter fileWriter = new FileWriter(fileName, true); mBufWriter = new BufferedWriter(fileWriter); mBufWriter.write(msg); mBufWriter.newLine(); mBufWriter.flush(); mBufWriter.close(); } catch (Throwable e) { try { mBufWriter.close(); } catch (Throwable t) {}; } return; }/** * 用来创建文件和文件夹 * @param fileName 文件或文件夹名称 * @return * @throws IOException * @throws Exception */ private static boolean createFile(String fileName)throws IOException ,Exception{ File file = new File(fileName); if (file.exists()) /* does file exist? If so, can it be written to */ { if (file.canWrite() == false) return false; } else { String path = null; /* Does not exist. Create the directories */ int firstSlash = fileName.indexOf(File.separatorChar); int finalSlash = fileName.lastIndexOf(File.separatorChar); if (finalSlash == 0) { /* error, not valid path */ } else if (finalSlash == 1) /* UNIX root dir */ { path = File.separator; } else if (firstSlash == finalSlash) { /* for example c:\ Then make sure slash is part of path */ path = fileName.substring(0,finalSlash+1); } else { path = fileName.substring(0,finalSlash); } File dir = new File(path); dir.mkdirs(); } return true; }} 如果不进行读写操作那么File myFile=new File("c:\","myfile.java");这样就可以了 关于编译软件 问个比较弱智的问题 匹配网址正则表达式 问啥是正则式 向数据库插入数据的问题 菜鸟急求一道JAVA题,高手们花几分钟提点一下小弟吧.不胜感激 for(Ball b:balls)这是嘛循环? 散分,200,大家平分了吧 再散些分,特别给象我从来还没有过爱情的人儿 一个java入门级别的指导 请问'' 和""有何不同? 怎么把int[]变成byte[],然后再变回来?(在线等)!!
filename="forum";
f=new File(path,filename+".txt");
创建新文件
入口参数:文件路径: String path
文件名: String name
文件内容: String value
返回参数:成功与否 : boolean
*/
public boolean TextChanger(String path,String name,String value) {
String filepath = path + "/" + name;
String val = value;
boolean bln = false;
try {
FileOutputStream out = null;
out = new FileOutputStream(filepath);
DataOutputStream textout = null;
textout = new DataOutputStream(out);
val = new String(val.getBytes("gb2312"),"8859_1") + "\r\n";
textout.writeBytes(val);
textout.close();
out.close();
bln = true;
} catch (Exception e) {
}
return bln;
}
package com.hoten.util;
import java.io.*;
import java.util.*;
/**
*
* <p>Title: LOG 日志记录</p>
* <p>Description:
* 此类主要用来记录系统中发生的重大事件,以及由于程序本身所产生的错误信息</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: hoten </p>
* @author lqf
* @version 1.0
*/
////////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////
public class Log{
/**
* 用来记录系统重大事件
* @param msg 重大事件
* @param fileName 日志文件的路径及名称
*/
public synchronized static void printEvent(String msg,String fileName)
{
msg = new String( "时间:"+Time.getTime(Time.YYMMDDhhmmss) + " 事件消息: " + msg);
if(fileName!=null) printToFile(msg,fileName);
else print(msg);
return;
}
/**
* 记录应程序本身发生的错误,主要给程序员观察。
* @param e 一个Exception
* @param mobile 用户手机号码
* @param msg 用户发送的消息
* @param fileName 日志文件的路径及名称
*/
public synchronized static void printError(Throwable e,String mobile,String msg,String fileName)
{
StringBuffer errors=new StringBuffer(100);
errors.append("时间:");
errors.append(Time.getTime(Time.YYMMDDhhmmssxxx));
errors.append(" 手机号码:");
errors.append(mobile);
errors.append(" 消息:");
errors.append(msg);
errors.append(" Exception: ");
if(fileName!=null) {
printToFile(errors.toString().trim(),fileName);
try {
e.printStackTrace(new PrintWriter(new FileWriter(fileName,true),true));//
}
catch (Exception ex) {
}
}
else print(errors.toString().trim());
return;
}
/**把错误消息打印到屏幕上
*
* @param msg 错误消息
*/
private static void print(String msg)
{
System.out.println(msg);
}
/**
* 把消息打印到指定文件
* @param msg 错误消息
* @param fileName 指定的文件
*/ private static void printToFile(String msg,String fileName) //打印到文件中
{
BufferedWriter mBufWriter = null;
try
{
if(!createFile(fileName)) return ;
FileWriter fileWriter = new FileWriter(fileName, true);
mBufWriter = new BufferedWriter(fileWriter); mBufWriter.write(msg);
mBufWriter.newLine(); mBufWriter.flush();
mBufWriter.close();
}
catch (Throwable e)
{
try { mBufWriter.close(); } catch (Throwable t) {};
}
return;
}
/**
* 用来创建文件和文件夹
* @param fileName 文件或文件夹名称
* @return
* @throws IOException
* @throws Exception
*/ private static boolean createFile(String fileName)throws IOException ,Exception{
File file = new File(fileName);
if (file.exists()) /* does file exist? If so, can it be written to */
{
if (file.canWrite() == false)
return false;
}
else
{
String path = null; /* Does not exist. Create the directories */ int firstSlash = fileName.indexOf(File.separatorChar);
int finalSlash = fileName.lastIndexOf(File.separatorChar); if (finalSlash == 0) { /* error, not valid path */ }
else if (finalSlash == 1) /* UNIX root dir */
{
path = File.separator;
}
else if (firstSlash == finalSlash)
{ /* for example c:\ Then make sure slash is part of path */
path = fileName.substring(0,finalSlash+1);
}
else
{ path = fileName.substring(0,finalSlash); } File dir = new File(path);
dir.mkdirs();
}
return true;
}
}
File myFile=new File("c:\","myfile.java");
这样就可以了