wangy 发表于 2021-6-30 22:14:39

【Java】读取与保存文件

import java.util.*;
import android.view.*;
import android.widget.*;
import java.io.*;
public class Main {
    public static void main(String[] args) {
      System.out.println("HelloWorld");
    }
    public static void fileput(String str){
                byte []aa=new byte;
                //实例化
                FileOutputStream textput = null;
                try {
                        textput=this.openFileOutput("文件名字.txt", MODE_APPEND);
            try {
                                textput.write(str.getBytes());
                        }catch (IOException e){
            }
      }catch (FileNotFoundException e){
                        try{
                                textput.close();//关闭输入流
                        }
                        catch (IOException ea){
            }
                }
    }
    public static void fileget() {
      FileInputStream opedoc = null;
      //实化读取文件输入流,失败返回
                try
{
                        opedoc = this.openFileInput("文件名字.txt");
                        //获得输入流
                        try
{
                                opedoc.read(aa);
                }
catch (IOException e)
{
                }
                }
catch (FileNotFoundException e)
{
                        try
{
                                opedoc.close();
                        }
catch (IOException en)
{
               
            }
                }
                //强制转换为String型,
                //因为读出来的是byte型
                return new String(aa);
      }
}
页: [1]
查看完整版本: 【Java】读取与保存文件