本文目录一览:
- Java中如何通过txt文件存储和取出数据?
- java怎么从txt文件中读取数据
- java如何读取txt文件内容?
- java读txt方法
- Java中通过txt文件存储和取出数据
- Java 如何读取txt文件的内容?
Java中如何通过txt文件存储和取出数据?
Java中读取txt文件可以使用File
类先创建一个对象,然后使用I/O操作进行读取或写入操作,示例如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class demo2 {
private static String path = "f:/demo1.txt";
private static File file;
static {
file = new File(path);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Student stu = new Student(1, "张三", 90);
writeDataToFile(file, stu);
readDataFromFile(file);
}
private static void readDataFromFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = "";
while ((str = reader.readLine()) != null) {
String[] stuInfo = str.split(",");
System.out.println("学号:" + stuInfo[0] + "姓名:" + stuInfo[1] + "score:" + stuInfo[2]);
}
}
private static void writeDataToFile(File file, Student stu) throws FileNotFoundException {
PrintWriter out = new PrintWriter(new FileOutputStream(file, true));
out.println(stu.toString());
out.close();
}
}
java怎么从txt文件中读取数据
package txt;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* 读取TXT数据
*/
public class ReadTxtUtils {
public static void main(String arg[]) {
try {
String encoding = "GBK"; // 字符编码(可解决中文乱码问题)
File file = new File("c:/aa.txt");
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTXT = null;
while ((lineTXT = bufferedReader.readLine()) != null) {
System.out.println(lineTXT.toString().trim());
}
read.close();
} else {
System.out.println("找不到指定的文件!");
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
}
}
Java读取TXT文件中的数据,每一行就是一个数,返回一个数组,代码如下:
List<Integer> list = new ArrayList<>();
BufferedReader br = new BufferedReader(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 = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
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 = 0; j < str.length(); j++) {
temp[i][j] = Integer.parseInt(str[j]);
}
i++;
}
这是d:\\a.txt
的数据,与“|”分割取数据出来,保存在变量a;b;c;d
里:
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[1];
c = str[2];
d = str[3];
}
}
java如何读取txt文件内容?
通常,可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可。 (1)JAVA 读取txt文件内容 (2)读取文件效果:
java读txt方法
1. 按行读取TXT文件
package zc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class readLine {
public static void main(String[] args) {
File file = new File("C:/zc.txt");
BufferedReader reader = null;
String tempString = null;
int line = 1;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
while ((tempString = reader.readLine()) != null) {
System.out.println("Line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2. 按字节读取TXT文件
package zc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class readerFileByChars {
public static void main(String[] args) {
File file = new File("c:/zc.txt");
InputStream in = null;
byte[] tempByte = new byte[1024];
int byteread = 0;
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
in = new FileInputStream(file);
while ((byteread = in.read(tempByte)) != -1) {
System.out.write(tempByte, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Java中通过txt文件存储和取出数据
如果是这样的话,你就先用String
的split
方法以,
为分隔符号分开,再replace("", "")
,这两个方法就可以得到你要的中间的数据了。有个缺点比较占用内存,或许你也可以去读文件读到,
的时候就将之前的存起来,然后再读下面的东西。思路而已,试试看吧~
Java 如何读取txt文件的内容?
Java读取txt文件内容。可以作如下理解: 首先获得一个文件句柄:
File file = new File();
file
即为文件句柄。接下来可以开始读取信息:
new FileInputStream(file)
需要使用InputStreamReader()
这个方法进行解读刚才装进来内存当中的数据:
new InputStreamReader(new FileInputStream(file))
同时使用BufferedReader()
的readLine()
方法读取txt文件中的每一行数据。
示例代码如下:
package com.campu;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
/**
* @author Java团长
* H20121012.java
* 2017-10-29 上午11:22:21
*/
public class H20121012 {
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
* @param filePath
*/
public static void readTxtFile(String filePath) {
try {
String encoding = "GBK";
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); // 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String argv[]) {
String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt";
readTxtFile(filePath);
}
}
我有一个微信公众号,经常会分享一些Java技术相关的干货文章,还有一些学习资源。 如果你需要的话,可以用微信搜索“Java团长”或者“javatuanzhang”关注。