JAVA 高手解决 从键盘上读入任意数量字符,并写到文本文件中,再把字符串复制到另外一个文件中 20
3个回答
展开全部
import java.io.*;
public class Demo {
File file=new File("f:\\demo.txt");//目的地文件
public void test()
{
try
{
FileWriter write=new FileWriter(file);
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(reader);
String s;
System.out.println("Input the string:");
s=buf.readLine();
while(!s.equals("quit"))//以“quit”结束
{
write.write(s+"\r\n");
write.flush();
s=buf.readLine();
}
reader.close();
write.close();
System.out.println("write finished");
}
catch(Exception e)
{
e.printStackTrace();
}
}
File copyfile=new File("f:\\copy.txt");//复制到另一个文件中
public void copy()
{
try
{
FileReader reader=new FileReader(file);
FileWriter writer=new FileWriter(copyfile);
int ch;
while((ch=reader.read())!=-1)
{
writer.write((char)ch);
writer.flush();
}
reader.close();
writer.close();
System.out.println("copy finished");
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
public class Demo {
File file=new File("f:\\demo.txt");//目的地文件
public void test()
{
try
{
FileWriter write=new FileWriter(file);
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(reader);
String s;
System.out.println("Input the string:");
s=buf.readLine();
while(!s.equals("quit"))//以“quit”结束
{
write.write(s+"\r\n");
write.flush();
s=buf.readLine();
}
reader.close();
write.close();
System.out.println("write finished");
}
catch(Exception e)
{
e.printStackTrace();
}
}
File copyfile=new File("f:\\copy.txt");//复制到另一个文件中
public void copy()
{
try
{
FileReader reader=new FileReader(file);
FileWriter writer=new FileWriter(copyfile);
int ch;
while((ch=reader.read())!=-1)
{
writer.write((char)ch);
writer.flush();
}
reader.close();
writer.close();
System.out.println("copy finished");
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
输入,输出流。
inputStream outputStream
inputStream outputStream
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.*;
import java.util.Scanner;
public class Filein {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
FileOutputStream fOS;
try {
fOS = new FileOutputStream("file.txt");
System.out.println("请输入数据(end 结束输入)");
while (true) {
String s = in.nextLine();
if (s.equalsIgnoreCase("end"))
break;
fOS.write(s.getBytes());
fOS.write("\n".getBytes());
}
fOS.close();
FileInputStream fIS = new FileInputStream("file.txt");
FileOutputStream fOS1 = new FileOutputStream("copy.txt");
int i = fIS.read();
while (i != -1) {
fOS1.write(i);
i = fIS.read();
}
fOS.close();
fOS1.close();
fIS.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.Scanner;
public class Filein {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
FileOutputStream fOS;
try {
fOS = new FileOutputStream("file.txt");
System.out.println("请输入数据(end 结束输入)");
while (true) {
String s = in.nextLine();
if (s.equalsIgnoreCase("end"))
break;
fOS.write(s.getBytes());
fOS.write("\n".getBytes());
}
fOS.close();
FileInputStream fIS = new FileInputStream("file.txt");
FileOutputStream fOS1 = new FileOutputStream("copy.txt");
int i = fIS.read();
while (i != -1) {
fOS1.write(i);
i = fIS.read();
}
fOS.close();
fOS1.close();
fIS.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询