怎么将一个java程序的结果输出到文本文档中,写一段代码,谢谢
发布网友
发布时间:2022-04-28 11:30
我来回答
共5个回答
热心网友
时间:2023-10-05 13:01
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class OutToTxt {
private static BufferedWriter writer;
private static Scanner sc;
public static void main(String[] args) {
File out = new File("./Out.txt");
if(!out.exists()) {
try {
out.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
writer = new BufferedWriter(new FileWriter(out));
} catch (IOException e) {
e.printStackTrace();
}
sc = new Scanner(System.in);
System.out.println("请输入文本内容,输入exit结束:");
try {
writer.write(""); // 清空文本
String split = "";
while(true) {
String line = sc.nextLine();
if(line.equalsIgnoreCase("exit")) {
break;
}
writer.append(split + line);
split = "\r\n";
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(null != writer) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
你可以运行后输入
Hello World!
This is my first application.
exit
热心网友
时间:2023-10-05 13:02
Java里照写
System.out.println();
在CMD窗口 Java (filesname) >>1.TXT
::这样可以在当前路径下建立1.TXT ,并将Java中打印到CMD窗口的内容写到1.TXT中。
热心网友
时间:2023-10-05 13:02
File file=new File("C:/ads.txt");
BufferedWriter bw=new BufferedWriter(new FileWriter(file));
bw.write(参数为String);
bw.close();
热心网友
时间:2023-10-05 13:03
File filename = new File("F:\\suncity.txt");
String a="测试";
byte data[] = a.getBytes("Unicode");
RandomAccessFile mm = null;
try
{
mm = new RandomAccessFile(filename,"rw");
mm.write(data);
}
catch (IOException e1)
{
e1.printStackTrace();
}
finally
{
if(mm!=null)
{
try
{
mm.close();
}
catch (IOException e2)
{
System.out.println("sss");
}
}
}
热心网友
时间:2023-10-05 13:03
直接用输出重定向就行了,没必要写代码.
热心网友
时间:2023-10-05 13:01
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class OutToTxt {
private static BufferedWriter writer;
private static Scanner sc;
public static void main(String[] args) {
File out = new File("./Out.txt");
if(!out.exists()) {
try {
out.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
writer = new BufferedWriter(new FileWriter(out));
} catch (IOException e) {
e.printStackTrace();
}
sc = new Scanner(System.in);
System.out.println("请输入文本内容,输入exit结束:");
try {
writer.write(""); // 清空文本
String split = "";
while(true) {
String line = sc.nextLine();
if(line.equalsIgnoreCase("exit")) {
break;
}
writer.append(split + line);
split = "\r\n";
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(null != writer) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
你可以运行后输入
Hello World!
This is my first application.
exit
热心网友
时间:2023-10-05 13:02
Java里照写
System.out.println();
在CMD窗口 Java (filesname) >>1.TXT
::这样可以在当前路径下建立1.TXT ,并将Java中打印到CMD窗口的内容写到1.TXT中。
热心网友
时间:2023-10-05 13:02
File file=new File("C:/ads.txt");
BufferedWriter bw=new BufferedWriter(new FileWriter(file));
bw.write(参数为String);
bw.close();
热心网友
时间:2023-10-05 13:03
File filename = new File("F:\\suncity.txt");
String a="测试";
byte data[] = a.getBytes("Unicode");
RandomAccessFile mm = null;
try
{
mm = new RandomAccessFile(filename,"rw");
mm.write(data);
}
catch (IOException e1)
{
e1.printStackTrace();
}
finally
{
if(mm!=null)
{
try
{
mm.close();
}
catch (IOException e2)
{
System.out.println("sss");
}
}
}
热心网友
时间:2023-10-05 13:03
直接用输出重定向就行了,没必要写代码.