IO的学习

本文提供多个Java编程示例,展示了如何使用文件输入输出流进行文件的读写操作,包括直接写入字符串到文件、从文件中读取内容并显示、通过按钮触发文件的写入与读取操作以及批量写入和读取文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
//文件输入流和文件输出流
public class FileStream {
    public static void main(String[] args) {
        File file = new File("word.txt");
        try {
            FileOutputStream out = new FileOutputStream(file);
            byte buy[] = "this is a dog".getBytes();
            out.write(buy);
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }

        try {
            FileInputStream in = new FileInputStream(file);
            byte byt[] = new byte[1024];
            int len = in.read(byt);
            System.out.println(new String(byt,0,len));
            in.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File file = new File("a.txt");
        if(file.exists()){
            String name = file.getName();
            long length = file.length();
            System.out.println(name);
            System.out.println(length);
            file.delete();
            System.out.println("文件存着,删除此文件");
        }else {
            try{
                file.createNewFile();
                System.out.println("文件不存在,创建此文件");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class Ftest extends JFrame {
    private static final long serialVersionUID = 1L;
    private JTextArea jTextArea = null;
    private JButton openButton  = null;
    private JButton closeButton = null;

    private JButton getOpenButton(){
        if(openButton == null){
            openButton = new JButton();
            openButton.setText("写入文件");
            openButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    File file = new File("word.txt");
                    try{
                        FileWriter out = new FileWriter(file);
                        String s = jTextArea.getText();
                        out.write(s);
                        out.close();
                    }catch (Exception e1){
                        e1.printStackTrace();
                    }
                }
            });
        }
        return openButton;
    }

    private JButton getCloseButton(){
        if(closeButton == null){
            closeButton = new JButton();
            closeButton.setText("读取文件");
            closeButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    File file = new File("word.txt");
                    try{
                        FileReader in = new FileReader(file);
                        char byt[] = new char[1024];
                        int len = in.read(byt);
                        jTextArea.setText(new String(byt,0,len));
                        in.close();
                    }catch (Exception e1){
                        e1.printStackTrace();
                    }
                }
            });
        }
        return closeButton;
    }

    public Ftest(){
        super();
        initialize();
    }

    private void initialize(){
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        setTitle("JFrame");
        setVisible(true);
        setSize(200,200);
        final JTextArea jf = new JTextArea(6,6);
        jTextArea = jf;
        c.add(jf);
        c.add(getOpenButton());
        c.add(getCloseButton());
    }



    public static void main(String[] args) {
        Ftest thisClass = new Ftest();
        thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        thisClass.setVisible(true);
    }

}
import java.io.*;

public class Student {

    public static void main(String[] args) {
        String content[] = {"好久不见","最近好吗","常联系"};
        File file = new File("word.txt");
        try{
            FileWriter fw = new FileWriter(file);
            BufferedWriter bufw = new BufferedWriter(fw);
            for (int k = 0; k < content.length; k++) {
                bufw.write(content[k]);
                bufw.newLine();
            }
            bufw.close();
            fw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            FileReader fr = new FileReader(file);
            BufferedReader bufr = new BufferedReader(fr);
            String s = null;
            int i = 0;
            while ((s = bufr.readLine()) != null){
                i++;
                System.out.println("第"+i+"行"+s);
            }
            bufr.close();
            fr.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩淼燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值