import java.io.BufferedReader; |
import java.io.FileInputStream; |
import java.io.FileReader; |
import java.io.IOException; |
import java.io.InputStream; |
import java.util.ArrayList; |
/** |
* java读取txt文件 |
*/ |
public class ReadTxt { |
/* |
* |
* 读取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; |
} |
/* |
* |
* 读取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; |
} |
/* |
* |
* 读取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()); |
} |
} |