JAVA从键盘输入文件内容和文件名,把文件内容保存到该文件中。

JAVA从键盘输入文件内容和文件名,把文件内容保存到该文件中。,第1张

import javaioBufferedReader;
import javaioBufferedWriter;
import javaioFileNotFoundException;
import javaioFileReader;
import javaioFileWriter;
import javaioIOException;
import javaioInputStreamReader;
public class SystemInTest
{
public static void main(String[] args)
{
try
{
InputStreamReader isr=new InputStreamReader(Systemin);
BufferedReader br=new BufferedReader(isr);
FileWriter fw=new FileWriter("F:\\io\\abctxt");
BufferedWriter bw=new BufferedWriter(fw);
Systemoutprintln("输入你要输入到文件内容,移over结束:");
String s;
while(true)
{
s=brreadLine();
if("over"equals(s))
break;
bwwrite(s);
bwnewLine();
}
}
catch (FileNotFoundException e)
{
Systemoutprintln(" *** 作的文件不存在");
}
catch(IOException e)
{
Systemoutprintln("发生IO *** 作异常");
}
finally
{
try
{
bwclose();
fwclose();
brclose();
isrclose();
}
catch(Exception ex) {}
}
}
}
释放资源的语句最好写在finally中,这样即使程序运行过程中抛出异常,对应的资源也可以得到及时释放

你是要把只是输入的数据写入Txt,还是要把控制台的数据写入Txt?

如果是第一种,那么如下:

package test;
import javaioBufferedWriter;
import javaioFile;
import javaioFileWriter;
import javaioIOException;
import javautilScanner;
public class Student{
 public static void main(String[] args) throws IOException{
 info stu = new info();
 }
}
class info{
 info() throws IOException{
  Scanner in = new Scanner(Systemin);
  Systemoutprint("请输入要输入得学生个数:");
  int num = innextInt();
  File f = new File("Studenttxt") ; //先定义文件
  fdeleteOnExit() ; //如果存在则删除
  fcreateNewFile() ;//创建新文件
  //定义文件输出流
  BufferedWriter bfw = new BufferedWriter(new FileWriter(f));
  bfwwrite(""+num); //输出学生的个数
  bfwnewLine() ; //写入一个换行符
  
  Systemoutprintln("请输入学生的:");
  for(int j = 1; j<=num; j++ ){
   Systemoutprintln("第"+j+"个:");
   Systemoutprint("姓名:");
   String name = innext();
   bfwwrite(name); //输出学生的姓名
  
   Systemoutprint("性别:");
   String sex = innext();
   bfwwrite(sex); //输出学生的性别
   Systemoutprint("年龄:");
   int age = innextInt();
   bfwwrite(""+age); //输出学生的年龄
   Systemoutprint("专业:");
   String object = innext();
   bfwwrite(object); //输出学生的专业
   bfwnewLine() ; //写入一个换行符
  }
  
  bfwflush();
  bfwclose();
 }
}

可以通过“FileOutputStream”创建文件并保存,举例:
OutputStreamWriter pw = null;//定义一个流
pw = new OutputStreamWriter(new FileOutputStream(“D:/testtxt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“testtxt”
pwwrite("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write
pwclose();//关闭流
bre = new BufferedReader(new FileReader("D:/testtxt"));//此时获取到的bre就是整个文件的缓存流
while ((str = brereadLine())!= null) // 判断最后一行不存在,为空结束循环
{
Systemoutprintln(str);//原样输出读到的内容
};
bre close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

点击下载,其实就是访问文件路径,通过流读取,然后再指定文件保存位置还是通过流保存
file(连接路径)>>input>>out>>file(保存位置)

可以不用那个方法,这样就能用io里的fileinputstream()来作了,是吧?
public void readFile()//用于读取文件内容
{
try
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = brreadLine()) != null)
{
txtEditsetText(txtEditgetText()+str+"\n");
}
brclose();
frclose();
}
catch(Exception ee)
{
eeprintStackTrace();
}
}
public void saveFile()//保存文件
{
try
{
FileWriter fw = new FileWriter(file);
fwwrite(txtEditgetText());
fwclose();
}
catch(Exception e)
{
eprintStackTrace();
}
}
我大致知道你的意思应该怎么作了,你给d出的button++事件就好了,代码不用说了吧,你自己想想吧!
我用给你作了下,
给你点代码:
public void open()throws IOException{

String filepath=jfc1getSelectedFile()getAbsolutePath();
FileReader fr=new FileReader(filepath);
BufferedReader br=new BufferedReader(fr);
String str;
while((str = brreadLine())!=null)
jtasetText(jtagetText()+str+"\n");
brclose();
frclose();

}
public void save()throws IOException{

String filepath2=jfc2getSelectedFile()getAbsolutePath();

FileWriter fw=new FileWriter(filepath2);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
pwprint(jtagetText());
bwclose();
fwclose();
}

Java是通过使用I/O文件 *** 作类,创建输入输出流,将数据保存在指定的路径下的文件里面。
示例代码:
import javaioFile;
import javaioFileOutputStream;
import javaioIOException;

public class WriteFileTest {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("D:/testtxt");//初始化file
fop = new FileOutputStream(file);//初始化输出流
// 若文件不存在,则创建它
if (!fileexists()) {
filecreateNewFile();
}
// 获取字节的内容数组
byte[] contentInBytes = contentgetBytes();
fopwrite(contentInBytes);//写出到指定路径文件中字节的内容数组
fopflush();
fopclose();
Systemoutprintln("Done");
} catch (IOException e) { //捕捉异常
eprintStackTrace();
} finally {
try {
if (fop != null) {
fopclose();
}
} catch (IOException e) { //捕捉异常
eprintStackTrace();
}
}
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13182989.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-06-17
下一篇 2023-06-17

发表评论

登录后才能评论

评论列表(0条)

保存