package com.asideal.struts.action.ConfigAction ;import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConfigAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out
.println("-------------WelecomePageConfigAction--------------");
DataFileForm cform = (DataFileForm) form;
String fileName = cform.getImportfile().getFileName();
System.out.println("fileName:" + fileName); InputStream is = null;
FileOutputStream fs = null; DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
//<%@ page contentType="text/html;charset=GB2312"%>
try {
is = cform.getImportfile().getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String currdir = request.getSession().getServletContext()
.getRealPath("/")
+ File.separator
+ "page"
+ File.separator
+ "welcomePages"
+ File.separator + df.format(new Date()) + fileName;
File dir = new File(currdir);
fs = new FileOutputStream(dir);
PrintWriter pw=new PrintWriter(fs);
pw.println("<%@ page contentType=\"text/html;charset=GB2312\"%>");//?
String data = null;
//一行行读取
while((data = br.readLine())!=null)
{
System.out.println(data);
}
//FileWriter fw = new FileWriter(dir);
pw.write(data);
pw.flush();
//byte[] b = new byte[2048];
//is.read(b);
//fs.write(b, 0, b.length);
//fs.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fs.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
request.setAttribute("uploadsu", "success");
return mapping.findForward("success");
}
}
上面的读取写入代码能给完善下不?
怎样将上传文件的后缀由.html改为.jsp? IO流很迷糊
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out
.println("-------------WelecomePageConfigAction--------------");
DataFileForm cform = (DataFileForm) form;
String fileName = cform.getImportfile().getFileName();
System.out.println("fileName:" + fileName);InputStream is = null;
FileOutputStream fs = null;DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
//<%@ page contentType="text/html;charset=GB2312"%>
try {
is = cform.getImportfile().getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String currdir = request.getSession().getServletContext()
.getRealPath("/")
+ File.separator
+ "page"
+ File.separator
+ "welcomePages"
+ File.separator + df.format(new Date()) + fileName;
File dir = new File(currdir);
fs = new FileOutputStream(dir);
PrintWriter pw=new PrintWriter(fs);
pw.println("<%@ page contentType=\"text/html;charset=GB2312\"%>");//?
String data = null;
//一行行读取
while((data = br.readLine())!=null)
{
System.out.println(data);
}//FileWriter fw = new FileWriter(dir); pw.write(data);
pw.flush();
//byte[] b = new byte[2048];
//is.read(b);
//fs.write(b, 0, b.length);
//fs.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fs.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}request.setAttribute("uploadsu", "success");
return mapping.findForward("success");
}
}
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package sample;import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.asideal.util.dataForm.DataFileForm;/**
* MyEclipse Struts Creation date: 09-05-2011
*
* XDoclet definition:
*
* @struts.action
*/
public class WelecomePageConfigAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out
.println("-------------WelecomePageConfigAction--------------");
DataFileForm cform = (DataFileForm) form;
String fileName = cform.getImportfile().getFileName();
System.out.println("fileName:" + fileName); InputStream is = null;
FileOutputStream fs = null; DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
//<%@ page contentType="text/html;charset=GB2312"%>
try {
is = cform.getImportfile().getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String currdir = request.getSession().getServletContext()
.getRealPath("/")
+ File.separator
+ "page"
+ File.separator
+ "welcomePages"
+ File.separator + df.format(new Date()) + fileName;
File dir = new File(currdir);
fs = new FileOutputStream(dir);
PrintWriter pw=new PrintWriter(fs);
pw.println("<%@ page contentType=\"text/html;charset=GB2312\"%>");
String data = null;
//涓?琛岃璇诲彇
while((data = br.readLine())!=null)
{
System.out.println(data);
}
//这部分不会写了
//FileWriter fw = new FileWriter(dir);
pw.write(data);
pw.flush();
//byte[] b = new byte[2048];
//is.read(b);
//fs.write(b, 0, b.length);
//fs.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fs.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
request.setAttribute("uploadsu", "success");
return mapping.findForward("success");
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;public class TestFile2 {
public static void main(String[] args) throws IOException {
//找到个例子 怎么修改把ming.txt内的内容写入hello.jsp? BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("C:/ming.txt")));
String data = null;
while ((data = br.readLine()) != null) {
//System.out.println("=="+data);
FileWriter fw = new FileWriter("C:/hello.jsp");
String s = "hello world"+data;
fw.write(s, 0, s.length());
fw.flush();
fw.close();
}
br.close();
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
// "C:/hello2.txt"));
// osw.write(s, 0, s.length());
// osw.flush();
// PrintWriter pw = new PrintWriter(new OutputStreamWriter(
// new FileOutputStream("C:/hello3.txt")), true);
// pw.println(s);
}
}
package sample;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;public class TestFile2 {
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("C:/ming.txt")));
String data = null;
String s = "hello world";
FileWriter fw = new FileWriter("C:/hello.jsp",true);
fw.write(s, 0, s.length());
while ((data = br.readLine()) != null) {
//System.out.println("=="+data);
String s2 = data+"\n";
fw.write(s2, 0, s2.length());
//fw.write(s2,0,s2.length());
}
fw.flush();
fw.close();
br.close();
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
// "C:/hello2.txt"));
// osw.write(s, 0, s.length());
// osw.flush();
// PrintWriter pw = new PrintWriter(new OutputStreamWriter(
// new FileOutputStream("C:/hello3.txt")), true);
// pw.println(s);
}
}
hello world一直以来想把2005年写的《再再论指针》修改一下,
hello world一直以来想把2005年写的《再再论指针》修改一下,因为经过了这么多年,对C/C++的理解与05年相比又有了一些进展。
但公司的工作一直很忙,没有时间进行修改工作。直到10月初的长假,才开始。
从放假到现在过去两个月了,一直利用空闲时间断断续续地修改,共写了两万字,
十章节。偶不是作家,两万字对偶来说已经是极限中的极限了,没有功劳也有苦劳
,没有苦劳也有疲劳吧,不过总算把这几年来对数组与指针的心得写成了一系列文章,
当然限于偶的水平,存在错误无法避免,希望跟各位朋友一起,共同讨论,共同进步!
红色部分写入了两次?怎么修改
得到下面的内容hello world 换行
一直以来想把2005年写的《再再论指针》修改一下,
因为经过了这么多年,对C/C++的理解与05年相比又有了一些进展。
但公司的工作一直很忙,没有时间进行修改工作。直到10月初的长假,才开始。
从放假到现在过去两个月了,一直利用空闲时间断断续续地修改,共写了两万字,
十章节。偶不是作家,两万字对偶来说已经是极限中的极限了,没有功劳也有苦劳
,没有苦劳也有疲劳吧,不过总算把这几年来对数组与指针的心得写成了一系列文章,
当然限于偶的水平,存在错误无法避免,希望跟各位朋友一起,共同讨论,共同进步!
fw.write(s, 0, s.length());
把while ((data = br.readLine()) != null) {上面的fw.write(s, 0, s.length());去掉
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;public class TestFile2 {
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("C:/ming.txt")));
String s = "hello world"+"\n";
FileWriter fw = new FileWriter("C:/hello.jsp",true);
fw.write(s, 0, s.length());
String data = null;
while ((data = br.readLine()) != null) {
String s2 = data+"\n";
fw.write(s2, 0, s2.length());
}
fw.flush();
fw.close();
br.close();
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
// "C:/hello2.txt"));
// osw.write(s, 0, s.length());
// osw.flush();
// PrintWriter pw = new PrintWriter(new OutputStreamWriter(
// new FileOutputStream("C:/hello3.txt")), true);
// pw.println(s);
}
}
出来了
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String currdir = request.getSession().getServletContext()
.getRealPath("/")
+ File.separator
+ "page"
+ File.separator
+ "welcomePages"
+ File.separator + df.format(new Date()) + fileName;
File dir = new File(currdir);
fs = new FileOutputStream(dir);
PrintWriter pw=new PrintWriter(fs);
pw.println("<%@ page contentType=\"text/html;charset=GB2312\"%>");
String data = null;
//一行行读取
while((data = br.readLine())!=null)
{
pw.println(data);
}
pw.flush();
pw.close();
br.close();
还有个问题 + File.separator + df.format(new Date()) + fileName;
File dir = new File(currdir);
fs = new FileOutputStream(dir);
PrintWriter pw=new PrintWriter(fs);因为fileName是html格式的,我要写入到就到jsp格式中 就是那个fs怎么处理???
fileName.split("\\.")[0].toString()+".jsp";