//DataInputStream in = new DataInputStream(new BufferedInputStream(System.in)); try{
String s=""c:\\1.txt;
File f = new File(s);
if(!f.isFile())
{
System.out.println("input is not a file!");
this.input();
return;
}
Vector x = new Vector();
int linenum=0;
float InvestAve=0;
BufferedReader inf = new BufferedReader(new FileReader(s));
String c = inf.readLine();
//System.out.println(c);
while(c!=null)
{
System.out.println(c);
}
String s=""c:\\1.txt;
File f = new File(s);
if(!f.isFile())
{
System.out.println("input is not a file!");
this.input();
return;
}
Vector x = new Vector();
int linenum=0;
float InvestAve=0;
BufferedReader inf = new BufferedReader(new FileReader(s));
String c = inf.readLine();
//System.out.println(c);
while(c!=null)
{
System.out.println(c);
}
{
try{
String s=path;
File f = new File(s);
if(!f.isFile())
{
System.out.println("input is not a file!");
return;
}
BufferedReader inf = new BufferedReader(new FileReader(s));
String c = inf.readLine();
while(c!=null)
{
System.out.println(c);
c = inf.readLine();
}
}
catch(Exception e)
{} }
先谢过!!!
你能给一个完整的代码吗????
import java.util.*;
import java.net.*;
import java.text.*;
public class temp
{
public static void main(String[] args)
{
temp r = new temp();
r.ReadFiles("c:\\1.txt"); } public void ReadFiles(String path)
{
try{
String s=path;
File f = new File(s);
if(!f.isFile())
{
System.out.println("input is not a file!");
return;
}
BufferedReader inf = new BufferedReader(new FileReader(s));
String c = inf.readLine();
while(c!=null)
{
System.out.println(c);
c = inf.readLine();
}
}
catch(Exception e)
{} }}
先谢过!!!
你能给一个一行一行写的吗?马上结账.....
import java.util.*;
import java.net.*;
import java.text.*;
public class temp
{
public static void main(String[] args)
{
temp r = new temp();
try
{
r.writefiles("c:\\2.txt",r.ReadFiles("c:\\1.txt"));
}catch(Exception e)
{} } public Vector ReadFiles(String path)
{
Vector v = new Vector();
try{
String s=path;
File f = new File(s);
if(!f.isFile())
{
System.out.println("input is not a file!");
//return;
}
BufferedReader inf = new BufferedReader(new FileReader(s));
String c = inf.readLine();
while(c!=null)
{
v.add(c);
System.out.println(c);
c = inf.readLine();
}
}
catch(Exception e)
{}
return v;
}
public void writefiles(String path,Vector vec) throws IOException
{
String s = path;
File nf = new File(path);
if(!nf.exists())
{
nf.createNewFile();
}
String tx ="";
FileWriter fw = new FileWriter(s,true);
for(int i = 0;i<vec.size();i++)
{
tx += vec.elementAt(i);
fw.write(tx+"\n");
}
//FileWriter fw = new FileWriter(s,true);
fw.close();
}}
import java.util.*;
import java.net.*;
import java.text.*;
public class temp
{
public static void main(String[] args)
{
temp r = new temp();
try
{
r.writefiles("c:\\2.txt",r.ReadFiles("c:\\1.txt"));
}catch(Exception e)
{} } public Vector ReadFiles(String path)
{
Vector v = new Vector();
try{
String s=path;
File f = new File(s);
if(!f.isFile())
{
System.out.println("input is not a file!");
//return;
}
BufferedReader inf = new BufferedReader(new FileReader(s));
String c = inf.readLine();
while(c!=null)
{
v.add(c);
System.out.println(c);
c = inf.readLine();
}
}
catch(Exception e)
{}
return v;
}
public void writefiles(String path,Vector vec) throws IOException
{
String s = path;
File nf = new File(path);
if(!nf.exists())
{
nf.createNewFile();
}
String tx ="";
FileWriter fw = new FileWriter(s,true);
for(int i = 0;i<vec.size();i++)
{
fw.write(vec.elementAt(i).toString()+"\n");
}
fw.close();
}}
唉!你的分真不容易呀!
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<html>
<head>
<title>读取写入文件</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body bgcolor="#ffffff">
<%
BufferedReader file;
//存放读取的每行的内容
Hashtable values=new Hashtable();
//读取的文件
String countFile1=request.getRealPath("count1.txt");
//写入的文件
String countFile2=request.getRealPath("count2.txt");
//读取文件
file=new BufferedReader(new FileReader(countFile1));
String readStr=null;
String writeStr="";
int i=1;
try{
readStr=file.readLine();
}catch(IOException e){
out.println("读取数据出错");
}
//输出文件的每一行数据,同时输出
while(readStr!=null){
if(readStr==null){
readStr="";
}else{
writeStr=readStr;
}
out.println("第"+i+"行:"+readStr);
out.println("<br>");
try{
readStr=file.readLine();
}catch(IOException e){
out.println("读取数据出错");
}
//存入到哈什表中
values.put("value"+i,writeStr);
i++;
}
//写文件
try{
PrintWriter pw;
pw=new PrintWriter(new FileOutputStream(countFile2));
for(int j=1;j<i;j++){
writeStr=(String)values.get("value"+j);
pw.println(writeStr);
}
pw.close();
}catch(IOException e){
out.println(e.getMessage());
}
%>
</body>
</html>
要求jsp文件和两个txt文件在同一目录下。
我的服务器采用的是resin,用tomcat也是一个道理,放在jsp该放的位置:)