java string txt文件_java 如何将txt文本中的文字写入string

展开全部

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.Scanner;

import javax.imageio.stream.FileImageInputStream;

public class Abc {

/**

* @param args

* @throws IOException

*/

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

// TODO Auto-generated method stub

start();

}

public static void start() throws IOException

{

showSelection();

int i=0;

Scanner scan=new Scanner(System.in);

i=scan.nextInt();

select(i);

}

public static void showSelection()

{

System.out.print("1.创建");

System.out.print(" 2.显示e68a843231313335323631343130323136353331333332626132");

System.out.print(" 3.重命名");

System.out.print(" 4.复制");

System.out.print(" 5.删除");

System.out.println(" 6.退出");

System.out.print("请输入");

}

public static void select(int i) throws IOException

{

switch (i) {

case 1:System.out.println("创建");creatFile();start();break;

case 2:viewFile();start();break;

case 3:renameFile();start();break;

case 4:copyFile();start();break;

case 5:delFile();start();break;

case 6: System.exit(0);break;

default:

break;

}

}

public static boolean creatFile()

{

Scanner scans=new Scanner(System.in);

System.out.println("请输入要创建的文件名");

String name=scans.nextLine();

File file=new File("D:\\"+name);

try{

file.createNewFile();

System.out.println("创建成功");

}

catch (Exception e) {

// TODO: handle exception

}

return true;

}

public static boolean viewFile() throws IOException

{

Scanner scans=new Scanner(System.in);

System.out.println("请输入要读取的文件名");

String name=scans.nextLine();

File file=new File("D:\\"+name);

BufferedReader br1=new BufferedReader(new FileReader("D:\\"+name));

String str="";

while((str=br1.readLine())!=null)

System.out.println(str);

br1.close();

br1=null;

return true;

}

public static boolean renameFile()

{

Scanner scans=new Scanner(System.in);

System.out.println("请输入要重命名的文件名");

String name=scans.nextLine();

System.out.println("请输入新的文件名");

String newname=scans.nextLine();

File newfile=new File("D:\\"+newname);

File file=new File("D:\\"+name);

try{

file.renameTo(newfile);

System.out.println("重命名成功");

}

catch (Exception e) {

// TODO: handle exception

}

return true;

}

public static boolean copyFile() throws IOException

{int byteread=0;

int bytesum=0;

Scanner scans=new Scanner(System.in);

System.out.println("请输入要复制的文件名");

String name=scans.nextLine();

File file=new File("D:\\"+name);

System.out.println("请输入新复制后的文件名");

String newname=scans.nextLine();

File newfile=new File("D:\\"+newname);

InputStream in=new FileInputStream(file);

FileOutputStream fs=new FileOutputStream(newname);

byte[] buffer=new byte[1444];

while((byteread=in.read(buffer))!=-1)

{

bytesum+=byteread;

fs.write(buffer,0,byteread);

}

in.close();

return true;

}

public static boolean delFile()

{

Scanner scans=new Scanner(System.in);

System.out.println("请输入要删除的文件名");

String name=scans.nextLine();

File file=new File("D:\\"+name);

try{

file.delete();

System.out.println("删除成功");

}

catch (Exception e) {

// TODO: handle exception

}

return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值