Java如何往服务器写数据 30
我想把一些数据保存到服务器上,比如写篇文章以Txt文件保存在服务器上,下次要用的时候再读取里面的内容。请问如何用Java实现呢?希望高手们能给个实例让我学习一下。...
我想把一些数据保存到服务器上,比如写篇文章以Txt文件保存在服务器上,下次要用的时候再读取里面的内容。请问如何用Java实现呢?希望高手们能给个实例让我学习一下。
展开
2个回答
展开全部
package com.temp.test;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test {
static long start=0;
static long end=0;
static long run;
static boolean view=true;
static SimpleDateFormat sdf = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
public static void start(){
start=System.currentTimeMillis();
}
public static void end(){
end=System.currentTimeMillis();
}
public static void run (HttpServletRequest request) throws IOException{
write(" -------------------");
write("xx"); // 写内容到d:/abc.txt
write(" -------------------\r\n");
}
}
public static void write(String content) throws IOException {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("d:/abc.txt", true)));
out.write(content+"\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test {
static long start=0;
static long end=0;
static long run;
static boolean view=true;
static SimpleDateFormat sdf = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
public static void start(){
start=System.currentTimeMillis();
}
public static void end(){
end=System.currentTimeMillis();
}
public static void run (HttpServletRequest request) throws IOException{
write(" -------------------");
write("xx"); // 写内容到d:/abc.txt
write(" -------------------\r\n");
}
}
public static void write(String content) throws IOException {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("d:/abc.txt", true)));
out.write(content+"\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询