当前位置:编程学习 > JAVA >>

Java读取txt文件

/*

  * 读取char

  */

  private String readtxt() throws IOException{

  BufferedReader br=new BufferedReader(new FileReader("d:/sql.txt"));

  String str="";

  String r=br.readLine();

  while(r!=null){

  str+=r;

  r=br.readLine();

  }

  return str;

  }

  Java代码

  /*

  * 读取char

  */

  private String readtxt() throws IOException{

  BufferedReader br=new BufferedReader(new FileReader("d:/sql.txt"));

  String str="";

  String r=br.readLine();

  while(r!=null){

  str+=r;

  r=br.readLine();

  }

  return str;

  }

  Java代码

  /*

  * 读取char

  */

  private String readtxt2() throws IOException{

  String str="";

  FileReader fr=new FileReader("d:/sql.txt");

  char[] chars=new char[1024];

  int b=0;

  while((b=fr.read(chars))!=-1){

  str+=String.valueOf(chars);

  }

  return str;

  }

  Java代码

  /*

  * 读取char

  */

  private String readtxt2() throws IOException{

  String str="";

  FileReader fr=new FileReader("d:/sql.txt");

  char[] chars=new char[1024];

  int b=0;

  while((b=fr.read(chars))!=-1){

  str+=String.valueOf(chars);

  }

  return str;

  }

  Java代码

  /*

  * 读取bytes

  */

  private Byte[] readtxt3() throws IOException{

  InputStream input=new FileInputStream("d:/sql.txt");

  byte[] b=new byte[1024];

  ArrayList<Byte> lsbytes=new ArrayList<Byte>();

  int n=0;

  while((n=input.read(b))!=-1){

  for(int i=0;i<n;i++){

  lsbytes.add(b[i]);

  }

  }

  return (Byte[])(lsbytes.toArray());

  }

  Java代码

  /*

  * 读取bytes

  */

  private Byte[] readtxt3() throws IOException{

  InputStream input=new FileInputStream("d:/sql.txt");

  byte[] b=new byte[1024];

  ArrayList<Byte> lsbytes=new ArrayList<Byte>();

  int n=0;

  while((n=input.read(b))!=-1){

  for(int i=0;i<n;i++){

  lsbytes.add(b[i]);

  }

  }

  return (Byte[])(lsbytes.toArray());

  }

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,