java文本讀取
1. java如何讀取一個txt文件的所有內容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException {
String fileContent = readFileContent("");
System.out.println(fileContent);
}
//參數string為你的文件名
private static String readFileContent(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = "";
StringBuilder sb = new StringBuilder();
while(content != null){
content = bf.readLine();
if(content == null){
break;
}
sb.append(content.trim());
}
bf.close();
return sb.toString();
}
}
求點贊為滿意回答。
2. Java 如何讀取txt文件的內容
能有的,很簡單,readLine即可,然後封裝到Map裡面,key就是序號,value就是後面的值
3. java如何讀取txt文件
如果對讀寫性能來要自求不是很高的話,讀取TXT文件很簡單。
不考慮文本文件的編碼的話,可以用一個FileReader讀取磁碟上的文件即可
如下:
FileReader fr = new FileReader("文件路徑");
然後循環調用fr.read();
將文本數據讀入一個char數組。
再解析數組中的字元,將網站名跟URL區分開,分別存入資料庫即可。
這是最簡單的實現了。
如果IO部分不熟悉,可以參考JDK文檔中FileReader類、InputStreamReader類等java.io包中相關類的文檔。
實際應用中,字元集編碼是個很重要的問題。
4. java讀取txt文件
importjava.io.File;
publicclassTest{
publicstaticvoidmain(String[]args){
try{
Filefile=newFile("info.txt");
newRead().readFile(file);
}catch(Exceptione){
e.printStackTrace();
}
}
}
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.util.ArrayList;
publicclassRead{
publicvoidreadFile(Filefile){
ArrayList<String>arrayList=newArrayList<>();
try{
BufferedReaderbufferedReader=newBufferedReader(newFileReader(file));
inti=1;
Stringline=null;
Stringperson="";
while((line=bufferedReader.readLine())!=null){
String[]strings=line.split("\s+");
for(Strings:strings){
if(i!=4){
person+=s+",";
}
else{
person+=s;
arrayList.add(person);
person="";
i=0;
}
i++;
}
}
System.out.println("{");
for(i=0;i<arrayList.size();i++){
Strings=arrayList.get(i);
if(i!=arrayList.size()-1)
System.out.print("["+s+"];");
else
System.out.print("["+s+"]");
}
System.out.println("}");
}catch(Exceptione){
e.printStackTrace();
}
}
}
5. 如何用java讀取txt文件
用java讀取txt文件:
public String read(String path) throws Exception { //讀
File f = new File(path);
FileInputStream input = new FileInputStream(f);
BufferedInputStream buf=new BufferedInputStream(input);
byte[] b=new byte[(int) f.length()];
input.read(b);
input.close();
return new String(b);
}
public static void writeFileByByte(String path,String strs,boolean a) throws Exception{ //寫
File f1=new File(path);
FileOutputStream out=new FileOutputStream(f1,a);
byte[] b=strs.getBytes();
out.write(b);
out.close();
}
也可以參考JAVA IO。
public class ReadTxtFile {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\795829\\Desktop\\1.txt");
// 字元流讀取文件數據
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
System.exit(-1); // TODO 測試用
// 位元組流讀取文件數據
FileInputStream fis = new FileInputStream(file);
int n = 1024;
byte buffer[] = new byte[n];
while ((fis.read(buffer, 0, n) != -1) && (n > 0)) {
System.out.print(new String(buffer));
}
fis.close();
}
}
6. java如何讀取txt文件內容
給你兩個方法,你可以看看;
//獲取值返回String文本
publicStringtxt2String(StringfilePath){
Filefile=newFile(filePath);
StringBuilderresult=newStringBuilder();
try{
BufferedReaderbr=newBufferedReader(newFileReader(file));//構造一個BufferedReader類來讀取文件
Strings=null;
while((s=br.readLine())!=null){//使用readLine方法,一次讀一行
result.append(s+System.lineSeparator());
}
br.close();
}catch(Exceptione){
e.printStackTrace();
}
returnresult.toString();
}
//獲取值不返回String文本
publicvoidreadTxtFile(StringfilePath){
try{
Stringencoding="GBK";
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){//判斷文件是否存在
InputStreamReaderread=newInputStreamReader(
newFileInputStream(file),encoding);//考慮到編碼格式
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=null;
while((lineTxt=bufferedReader.readLine())!=null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}catch(Exceptione){
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}
}
7. java文件讀取
String
path
=
System.getProperty("user.dir")//得到當前工作源路徑
File
fileData;
FileReader
fr;
BufferedReader
br;
fileData=new
File(path,file);
//file如1.txt文件名.需要在path即工作路徑下.path為工作路徑
也可以改為其他
try
{
fr=new
FileReader(fileData);
br=new
BufferedReader(fr);
}
catch
(Exception
ex)
{
ex.printStackTrace();
}
String
result
=
br.readLine();
然後將result中數據(如果為數字則可將字元取出轉化為數字再排序)操作
8. java怎樣實現讀寫TXT文件
主要有用到java原生態的Io類,沒有第三個包。直接上代碼:
importjava.io.*;
publicclasswrite{
publicstaticvoidmain(String[]args){
write("E://123.txt","hello");
}
publicstaticvoidwrite(Stringpath,Stringcontent){
try{
Filef=newFile(path);
if(f.exists()){
System.out.println("文件存在");
}else{
System.out.println("文件不存在,正在創建...");
if(f.createNewFile()){
System.out.println("文件創建成功!");
}else{
System.out.println("文件創建失敗!");
}
}
BufferedWriteroutput=newBufferedWriter(newFileWriter(f));
output.write(content);
output.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
9. java中如何從文件中讀取數據
分為讀位元組,讀字元兩種讀法
◎◎◎FileInputStream 位元組輸入流讀文件◎◎◎
public class Maintest {
public static void main(String[] args) throws IOException {
File f=new File("G:\\just for fun\\xiangwei.txt");
FileInputStream fin=new FileInputStream(f);
byte[] bs=new byte[1024];
int count=0;
while((count=fin.read(bs))>0)
{
String str=new String(bs,0,count); //反復定義新變數:每一次都 重新定義新變數,接收新讀取的數據
System.out.println(str); //反復輸出新變數:每一次都 輸出重新定義的新變數
}
fin.close();
}
}
◎◎◎FileReader 字元輸入流讀文件◎◎◎
public class Maintest {
public static void main(String[] args) throws IOException {
File f=new File("H:\\just for fun\\xiangwei.txt");
FileReader fre=new FileReader(f);
BufferedReader bre=new BufferedReader(fre);
String str="";
while((str=bre.readLine())!=null) //●判斷最後一行不存在,為空
{
System.out.println(str);
}
bre.close();
fre.close();
}
}
10. java中讀取一個txt文本內容
import java.io.*;
public class FileReaderDemo {
public static void main(String[] args) {
fr=null;
try{
fr=new FileReader("text.txt");
char[] buf=new char[1024];
int len=0;
while((len=fr.read(buf))!=-1){
System.out.print(new String(buf,0,len));
}
}catch(IOException e){
System.out.println(e.toString());
}finally{
if(fr!=null){
try{
fr.close();
}catch(IOException e){
System.out.println("close:"+e.toString());
}
}
}
/*一次讀一個字元
int ch=0;
while((ch=fr.read())!=-1){
System.out.print((char)ch);
}*/
}