用java代码把内容写到Word文档?
答案:3 悬赏:0 手机版
解决时间 2021-11-23 04:58
- 提问者网友:容嬷嬷拿针来
- 2021-11-22 04:39
用java代码把内容写到Word文档?
最佳答案
- 五星知识达人网友:大漠
- 2021-11-22 05:01
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Test {
public static void main(String[] args) {
try
{
FileInputStream fr=new FileInputStream("test/123.txt");
InputStreamReader af=new InputStreamReader(fr);
BufferedReader bf=new BufferedReader(af);
FileOutputStream fo=new FileOutputStream("test/456.doc");
OutputStreamWriter osw=new OutputStreamWriter(fo);
BufferedWriter bw=new BufferedWriter(osw);
String line=bf.readLine();
while(line!=null)
{
System.out.println(line);
bw.write(line);
bw.newLine();
bw.flush();
line=bf.readLine();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
test/123.txt里的内容写到test/456.doc下了,在你的java src中建一个test文件夹,这里是相对路径
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Test {
public static void main(String[] args) {
try
{
FileInputStream fr=new FileInputStream("test/123.txt");
InputStreamReader af=new InputStreamReader(fr);
BufferedReader bf=new BufferedReader(af);
FileOutputStream fo=new FileOutputStream("test/456.doc");
OutputStreamWriter osw=new OutputStreamWriter(fo);
BufferedWriter bw=new BufferedWriter(osw);
String line=bf.readLine();
while(line!=null)
{
System.out.println(line);
bw.write(line);
bw.newLine();
bw.flush();
line=bf.readLine();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
test/123.txt里的内容写到test/456.doc下了,在你的java src中建一个test文件夹,这里是相对路径
全部回答
- 1楼网友:渡鹤影
- 2021-11-22 06:19
public static void outputString1(String str, File file) {
try {
out = new PrintWriter(file);
byte bytes[] = str.getBytes();
int i = 0;
while ((bytes.length - i) != 0) {
out.write(bytes[i]);
i++;
}
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
* @param str
* 要写入文件的字符串
* @param file
* 要写入字符串的文件名,如new File("C:/test.doc")
try {
out = new PrintWriter(file);
byte bytes[] = str.getBytes();
int i = 0;
while ((bytes.length - i) != 0) {
out.write(bytes[i]);
i++;
}
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
* @param str
* 要写入文件的字符串
* @param file
* 要写入字符串的文件名,如new File("C:/test.doc")
- 2楼网友:蕴藏春秋
- 2021-11-22 05:29
使用java的输入输出流,进行读取,然后写入到文档中。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯