2.
3.import java.io.BufferedReader
4.import java.io.File
5.import java.io.FileInputStream
6.import java.io.InputStreamReader
7.
8./**
9. * 读取TXE数据
10. */
11.public class ReadTxtUtils {
12.public static void main(String arg[]) {
13.try {
14.String encoding = "GBK"// 字符编码(可解决中文乱码问题 )
15.File file = new File("c:/aa.txt")
16.if (file.isFile() &&file.exists()) {
17.InputStreamReader read = new InputStreamReader(
18.new FileInputStream(file), encoding)
19.BufferedReader bufferedReader = new BufferedReader(read)
20.String lineTXT = null
21.while ((lineTXT = bufferedReader.readLine()) != null) {
22. System.out.println(lineTXT.toString().trim())
23.}
24.read.close()
25.}else{
26.System.out.println("找不到指定的文件!")
27.}
28.} catch (Exception e) {
29.System.out.println("读取文件内容操作出错")
30.e.printStackTrace()
31.}
32.}
33.}
java读取TXT文件中的数据,每一行就是一个数,返回一个数组,代码?
?
List list=new ArrayList()
BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File("in.txt"))))
String str=null
while((str=br.readLine())!=null)
{
list.add(new Integer(str))
}
Integer[] i=new Integer[list.size()]
list.toArray(i)
TXT文本中如据形如:
123
456
789
读入二维数组效果为:
temp[0][]={1,2,3}
temp[1][]={4,5,6}
temp[2][]={7,8,9}
import java.io.BufferedReader
import java.io.FileNotFoundException
import java.io.FileReader
import java.io.*
public class xx{
public static void main(String[]args){
String s
int[][]save=new int[3][3]
try{
BufferedReader in =new BufferedReader(new FileReader("C:\\txt.txt"))
int i=0
while((s=in.readLine())!=null){
save[i][0]=Integer.parseInt(s.substring(0,1))
save[i][1]=Integer.parseInt(s.substring(1,2))
save[i][2]=Integer.parseInt(s.substring(2,3))
i++
}
}
catch(FileNotFoundException e){
e.printStackTrace()
}
catch(IOException e){
e.printStackTrace()
}
for(int i=0i<3i++)
{
for(int j=0j<3j++){
System.out.print(save[i][j])
}
System.out.println()
}
}
}
或
?
BufferedReader bf=new BufferedReader(new FileReader("Your file"))
String lineContent=null
int i = 0
int [][] temp = new int [3][]
while((lineContent=bf.readLine())!=null){
String [] str = lineContent.split("\\d")// 将 lineContent 按数字拆分
for(int j = 0j <str.length()j++){
int [i][j] = Integer.parseInt(str[j])
}
i++
}
scp|cs|ff|201101
这是d:\\a.txt的数据,与“|”分割取数据出来,保存在变量abcd里
import java.io.*
public class Test{
public static void main(String[] args)throws Exception{
String a, b, c, d
StringBuffer sb = new StringBuffer()
BufferedReader br = new BufferedReader(new FileReader("d:\\a.txt"))
String s = br.readLine()
while(s != null){
sb.append(s)
s = br.readLine()
}
s = sb.toString()
String[] str = s.split("|")
a = str[0]
b = str[0]
c = str[0]
d = str[0]
}
}
这个JDK的类库都帮我们实现好了。如FileWriter类:
public FileWriter(File file,boolean append)
throws IOException
根据给定的 File 对象构造一个 FileWriter 对象。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
例子程序如下:
import java.io.File
import java.io.Writer
import java.io.FileWriter
public class WriterDemo02{
public static void main(String args[]) throws Exception}
java的优点:
java是纯面向对象编程的语言;
平台无关性 (一次编译,到处运行;Write Once,Run Anywhere);
java提供了许多内置的类库,通过这些类库,简化了开发人员的设计工作,同时缩短了项目开发时间;
提供了对Web应用开发的支持,例如,Applet,Servlet,和JSP可以用来开发Web应用程序,Socket,RMI可以用来开发分布式应用程序的类库。