java如何把字符串的内容保存到某位置的txt里?
importjava.util.Scanner;importjavax.swing.JOptionPane;publicclassExam{publicstaticvoi...
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Exam{
public static void main(String args[]) {
String input1 =JOptionPane.showInputDialog(null,
"请输入起始网页链接",
"老江说",
JOptionPane.QUESTION_MESSAGE);
String input2 =JOptionPane.showInputDialog(null,
"请输入目标网页链接",
"老江说",
JOptionPane.QUESTION_MESSAGE);
String input3 =JOptionPane.showInputDialog(null,
"请输入最大搜索长度",
"老江说",
JOptionPane.QUESTION_MESSAGE);
Scanner input = new Scanner(input1+"\n"+input2+"\n"+input3);
while (input.hasNext()) {
String a = input.next();
System.out.println(a);
}
input.close();
}
}
已经编写以上的东东,就是要把input1、input2、input3的内容保存到C盘目录下的link.txt里面。。。
需要加什么代码?
请详细写出,谢谢!
请详细写出,最好是那种可以复制粘贴直接用的那种 展开
import javax.swing.JOptionPane;
public class Exam{
public static void main(String args[]) {
String input1 =JOptionPane.showInputDialog(null,
"请输入起始网页链接",
"老江说",
JOptionPane.QUESTION_MESSAGE);
String input2 =JOptionPane.showInputDialog(null,
"请输入目标网页链接",
"老江说",
JOptionPane.QUESTION_MESSAGE);
String input3 =JOptionPane.showInputDialog(null,
"请输入最大搜索长度",
"老江说",
JOptionPane.QUESTION_MESSAGE);
Scanner input = new Scanner(input1+"\n"+input2+"\n"+input3);
while (input.hasNext()) {
String a = input.next();
System.out.println(a);
}
input.close();
}
}
已经编写以上的东东,就是要把input1、input2、input3的内容保存到C盘目录下的link.txt里面。。。
需要加什么代码?
请详细写出,谢谢!
请详细写出,最好是那种可以复制粘贴直接用的那种 展开
展开全部
通过scanner对象进行写入,以下是定义的方法,直接调用即可
public writeStrToFile(String str,String path)//要写入的字符串,要写入的文件路径
{
Scanner input = new Scanner(str+"\n");
FileOutputStream fos = new FileOutputStream(path);
while (input.hasNext()) {
String a = input.next();
fos.write((a + "\r\n").getBytes());
}
fos.close();
input.close();
}
展开全部
import java.io.FileOutputStream;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Exam {
public static void main(String args[]) throws Exception {
String input1 = JOptionPane.showInputDialog(null, "请输入起始网页链接", "老江说",
JOptionPane.QUESTION_MESSAGE);
String input2 = JOptionPane.showInputDialog(null, "请输入目标网页链接", "老江说",
JOptionPane.QUESTION_MESSAGE);
String input3 = JOptionPane.showInputDialog(null, "请输入最大搜索长度", "老江说",
JOptionPane.QUESTION_MESSAGE);
Scanner input = new Scanner(input1 + "\n" + input2 + "\n" + input3);
FileOutputStream fos = new FileOutputStream("c:\\link.txt");
while (input.hasNext()) {
String a = input.next();
fos.write((a + "\r\n").getBytes());
}
fos.close();
input.close();
}
}
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Exam {
public static void main(String args[]) throws Exception {
String input1 = JOptionPane.showInputDialog(null, "请输入起始网页链接", "老江说",
JOptionPane.QUESTION_MESSAGE);
String input2 = JOptionPane.showInputDialog(null, "请输入目标网页链接", "老江说",
JOptionPane.QUESTION_MESSAGE);
String input3 = JOptionPane.showInputDialog(null, "请输入最大搜索长度", "老江说",
JOptionPane.QUESTION_MESSAGE);
Scanner input = new Scanner(input1 + "\n" + input2 + "\n" + input3);
FileOutputStream fos = new FileOutputStream("c:\\link.txt");
while (input.hasNext()) {
String a = input.next();
fos.write((a + "\r\n").getBytes());
}
fos.close();
input.close();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Exam{
public static void main(String[] args) {
String input1 = JOptionPane.showInputDialog(null, "请输入起始网页链接", "老江说",
JOptionPane.QUESTION_MESSAGE);
String input2 = JOptionPane.showInputDialog(null, "请输入目标网页链接", "老江说",
JOptionPane.QUESTION_MESSAGE);
String input3 = JOptionPane.showInputDialog(null, "请输入最大搜索长度", "老江说",
JOptionPane.QUESTION_MESSAGE);
Scanner input = new Scanner(input1 + "\n" + input2 + "\n" + input3);
FileOutputStream fos = null;
PrintWriter out = null;
try{
File file = new File("C:/link.txt");
fos = new FileOutputStream(file);
out = new PrintWriter(fos);
while(input.hasNext()){
out.println(input.next());
}
}catch (Exception e) {
e.printStackTrace();
}
finally{
if(out!=null)
out.close();
try {
if(fos!=null)
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Exam{
public static void main(String[] args) {
String input1 = JOptionPane.showInputDialog(null, "请输入起始网页链接", "老江说",
JOptionPane.QUESTION_MESSAGE);
String input2 = JOptionPane.showInputDialog(null, "请输入目标网页链接", "老江说",
JOptionPane.QUESTION_MESSAGE);
String input3 = JOptionPane.showInputDialog(null, "请输入最大搜索长度", "老江说",
JOptionPane.QUESTION_MESSAGE);
Scanner input = new Scanner(input1 + "\n" + input2 + "\n" + input3);
FileOutputStream fos = null;
PrintWriter out = null;
try{
File file = new File("C:/link.txt");
fos = new FileOutputStream(file);
out = new PrintWriter(fos);
while(input.hasNext()){
out.println(input.next());
}
}catch (Exception e) {
e.printStackTrace();
}
finally{
if(out!=null)
out.close();
try {
if(fos!=null)
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
OutputStream输出
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用一下输出流 outputstream
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询