当前位置: 技术问答>java相关
Bean中文件操作问题
来源: 互联网 发布时间:2015-07-04
本文导语: 1.Bean中能不能对服务器端文件进行操作? 2.我现在在Bean中得某个方法中想对某个文件操作,但报错,如下 【code】 public void WriteFile(String filePath) throws FileNotFoundException { try { PrintWriter pw = new PrintWriter(new ...
1.Bean中能不能对服务器端文件进行操作?
2.我现在在Bean中得某个方法中想对某个文件操作,但报错,如下
【code】
public void WriteFile(String filePath) throws FileNotFoundException
{
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
pw.println("offline");
pw.close();
} catch(IOException e) {
//错误处理
System.out.println("写入文件错误"+e.getMessage());
}
}
【/code】
在方法中调用这个WriteFile如
WriteFile("d:/myweb/a.txt");
但报错说:
D:MyWebWEB-INFclassesonLineUser.java:58: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
WriteFile("d:\myweb\a.txt");
请问怎么解决~~~~
谢谢了先
2.我现在在Bean中得某个方法中想对某个文件操作,但报错,如下
【code】
public void WriteFile(String filePath) throws FileNotFoundException
{
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
pw.println("offline");
pw.close();
} catch(IOException e) {
//错误处理
System.out.println("写入文件错误"+e.getMessage());
}
}
【/code】
在方法中调用这个WriteFile如
WriteFile("d:/myweb/a.txt");
但报错说:
D:MyWebWEB-INFclassesonLineUser.java:58: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
WriteFile("d:\myweb\a.txt");
请问怎么解决~~~~
谢谢了先
|
try:
import java.io.*;
import java.util.*;
import java.text.*;
public class WriteFile
{
public static String writeTxt(StringBuffer txtConcent,String filePath,String filePreName)
{
String fileName=filePreName+".txt";
String fullFilePath=filePath+fileName;
try
{
PrintWriter pw = new PrintWriter(new FileWriter(fullFilePath, false), true);
pw.println(txtConcent.toString());
}
catch (Exception e)
{
System.out.println("error");
}
return fileName;
}
}
import java.io.*;
import java.util.*;
import java.text.*;
public class WriteFile
{
public static String writeTxt(StringBuffer txtConcent,String filePath,String filePreName)
{
String fileName=filePreName+".txt";
String fullFilePath=filePath+fileName;
try
{
PrintWriter pw = new PrintWriter(new FileWriter(fullFilePath, false), true);
pw.println(txtConcent.toString());
}
catch (Exception e)
{
System.out.println("error");
}
return fileName;
}
}