java.txt的简单介绍
本篇文章给大家谈谈java.txt,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
如何用Java读取一个txt文件,并将文件内容保存到String类型的变量中?
importjava.io.BufferedReader;\x0d\x0aimportjava.io.File;\x0d\x0aimportjava.io.FileReader;\x0d\x0aimportjava.io.IOException;\x0d\x0a\x0d\x0apublicclassReadFile{\x0d\x0a\x0d\x0apublicstaticvoidmain(String[]args)throwsIOException{\x0d\x0a\x0d\x0aStringfileContent=readFileContent("");\x0d\x0a\x0d\x0aSystem.out.println(fileContent);\x0d\x0a}\x0d\x0a\x0d\x0a//参数string为你的文件名\x0d\x0aprivatestaticStringreadFileContent(StringfileName)throwsIOException{\x0d\x0a\x0d\x0aFilefile=newFile(fileName);\x0d\x0a\x0d\x0aBufferedReaderbf=newBufferedReader(newFileReader(file));\x0d\x0a\x0d\x0aStringcontent="";\x0d\x0aStringBuildersb=newStringBuilder();\x0d\x0a\x0d\x0awhile(content!=null){\x0d\x0acontent=bf.readLine();\x0d\x0a\x0d\x0aif(content==null){\x0d\x0abreak;\x0d\x0a}\x0d\x0a\x0d\x0asb.append(content.trim());\x0d\x0a}\x0d\x0a\x0d\x0abf.close();\x0d\x0areturnsb.toString();\x0d\x0a}\x0d\x0a}
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方法
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
关于java.txt和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。