java如何向文本写入_我如何使用java写入文本文件?

这篇博客探讨了在Java中如何将应用程序输入写入文本文件,特别是使用`Learning Java 2nd Edition`一书中的示例。作者在创建一个`AddProperty`类时遇到困难,希望将用户输入保存到文本文件中,以便后续读取属性细节。代码展示了一个包含文本字段和提交按钮的简单GUI,但目前尚不清楚如何将输入数据写入文件。在`ActionListener`中,需要实现将用户输入写入文本文件的功能。
摘要由CSDN通过智能技术生成

我一直在使用“Learning Java 2nd Edtion”一书来尝试让我的Java应用程序将我的输入写入名为properties的文本文件。我已经将教科书示例操纵到我自己的代码中,但仍然有问题试图使其工作。我想我可能需要将它与我的提交按钮挂钩,但本章没有提及。

基本上我试图将信息存储在文本文件中,然后在另一个位置使用该文本文件来读取所有的属性细节。

这是我的代码为AddProperty页面迄今为止任何意见将不胜感激。

此刻四号撞墙。

/**

*

* @author Graeme

*/

package Login;

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.border.EmptyBorder;

public class AddProperty

{

public void AddProperty()

{

JFrame frame = new JFrame("AddPropertyFrame");

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// having to set sizes of components is rare, and often a sign

// of problems with layouts.

//frame.setSize(800,600);

JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));

// make it big like the original

panel.setBorder(new EmptyBorder(100,20,100,20));

frame.add(panel);

//panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

JLabel HouseNumber = new JLabel("House Number/Name");

panel.add(HouseNumber);

JTextField HouseNumber1 = new JTextField(10);

panel.add(HouseNumber1);

JLabel HousePrice = new JLabel("House Price");

panel.add(HousePrice);

JTextField HousePrice1 = new JTextField(10);

panel.add(HousePrice1);

JLabel HouseType = new JLabel("House Type");

panel.add(HouseType);

JTextField HouseType1 = new JTextField(10);

panel.add(HouseType1);

JLabel Location = new JLabel("Location");

panel.add(Location);

JTextField Location1 = new JTextField(10);

panel.add(Location1);

JButton submit = new JButton("Submit");

panel.add(submit);

submit.addActionListener(new Action());

// tell the GUI to assume its natural (minimum) size.

frame.pack();

}

static class Action implements ActionListener{

@Override

public void actionPerformed (ActionEvent e)

{

// this should probably be a modal JDialog or JOptionPane.

JOptionPane.showMessageDialog(null, "You have successfully submitted a property.");

}

static class propertyList

{

public static void main (String args[]) throws Exception {

File properties = new File(args[0]);

if (!properties.exists() || !properties.canRead() ) {

System.out.println("Cant read " + properties);

return;

}

if (properties.isDirectory()){

String [] properties1 = properties.list();

for (int i=0; i< properties1.length; i++)

System.out.println();

}

else

try {

FileReader fr = new FileReader (properties);

BufferedReader in = new BufferedReader (fr);

String line;

while ((line = in.readLine())!= null)

System.out.println(line);

}

catch (FileNotFoundException e){

System.out.println("Not Able To Find File");

}

}

}

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用字符流和字节流来写入文本文件。字符流适用于处理文本内容,而字节流适用于处理二进制数据。你可以根据具体的需求选择适合的方法。 一种常见的字符流写入文本文件的方法是使用FileWriter类。你只需要传入具体的文件路径和待写入的内容即可。下面是一个示例代码: ```java import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { fileWriterMethod("/Users/mac/Downloads/io_test/write1.txt","Hello,Java."); } public static void fileWriterMethod(String filepath, String content) { try (FileWriter fileWriter = new FileWriter(filepath)) { fileWriter.write(content); } catch (IOException e) { e.printStackTrace(); } } } ``` 另一种常见的字节流写入文本文件的方法是使用BufferedOutputStream类。你可以使用它的write方法将内容写入文件。下面是一个使用BufferedOutputStream写入文本文件的示例代码: ```java import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) { try { bufferedOutputStreamMethod("/Users/mac/Downloads/io_test/write1.txt", "Hello,Java."); } catch (IOException e) { e.printStackTrace(); } } public static void bufferedOutputStreamMethod(String filepath, String content) throws IOException { try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filepath))) { bufferedOutputStream.write(content.getBytes()); } } } ``` 以上就是两种常见的Java写入文本文件的方法。你可以根据具体的情况选择适合自己的方法来实现文件写入操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值