java读取txt文件编码|java怎样设置txt文件的编码格式

|

① 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(); }}求采纳为满意回答。

② Java如何读写txt文件的代码

有关Java如何读写txt文件这个问题经常在面试时会被问到,不懂或不熟悉的同志们可是要记好了哟!先来看下具体实现吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 读取数据 */ public static void ReadDate() { String url = “e:/2.txt”; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while(d!=-1){ String str = new String(ch,0,d); sb.append(str); d = read.read(ch); } System.out.print(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 写入数据 */ public static void WriteDate() { try{ File file = new File(“D:/abc.txt”); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(file)); ArrayList ResolveList = new ArrayList(); for (int i = 0; i < 10; i++) { ResolveList.add(Math.random()* 100); } for (int i=0 ;i output.write(String.valueOf(ResolveList.get(i)) + “\n”); } output.close(); } catch (Exception ex) { System.out.println(ex); } } }

③ 怎样用Java读取TXT文本乱码解决方案

这个方法很简单,就是获取txt文件中的一行数据,然后转换为数组,我这个文本中只有一行。就这种情况下获取到的str是乱码,我想出现乱码肯定是字符集编码的问题,然后自己新建了一个文本文档测试没有问题(开始用的文本时别的程序导出的),所以我估计是开始那个txt文件编码的问题,查看发现是Unicode编码(如何查看txt的编码呢,教你一个笨笨的办法:打开文件——另存为,然后看到最下面的编码,默认选择的就是当前文档的编码格式),问题就出现在这里。问题找到了就好解决了,修改代码为:

privateString[] getProjectData(File file){

String[] data =null;

try{

BufferedReader br =newBufferedReader(newInputStreamReader(newFileInputStream(file),"UTF-16"));

String str = br.readLine();

data = str.split("");

br.close();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

returndata;

}

使用文件流读取,转换编码为“UTF-16”,这样乱码问题就解决了。

④ 一个文件夹下的多个txt文件,然后随机读取其中一个txt文件的内容(java代码)

提供个思路:1、把文件夹下所有txt文件的文件名,读取List里。2、生成一个随机数,随机的范围是:0到List.size()-1。3、用步骤2生产的随机数取个文件名。List.get(随机数变量)。4、根据步骤3中取到的文件名,去读取文件内容。这样就可以随机读取其中一个txt文件的内容了。

⑤ 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();}}

⑥ java读取txt文件时候出现乱码

WINDOW默认是GBK 看你java文件编码是什么 编码格式不统一 public static void main(String[] args)throws IOException{ FileInputStream fis = new FileInputStream( "D:\\StoreHouse.txt"); InputStreamReader isr = new InputStreamReader(fis, "GBK"); BufferedReader br=new BufferedReader(isr); StringBuffer SB=new StringBuffer(4096); String temp = null; while((temp=br.readLine())!=null){ SB.append(temp).append('\n'); System.out.println(SB); } br.close(); }

⑦ 用java 读取的txt文件为什么是乱码

注意UTF-8和Unicode之间的转换;如果是编码问题,可内以这样解决:容FileReader fr=new FileReader(file);BufferedReader br=new BufferedReader(fr);StringBuffer SB=new StringBuffer();while((temp=br.readLine())!=null){ SB.append(temp);}String strOut=new String(SB.getBytes("ISO-8859-1"),"UTF-8");System.out.println(strOut);

⑧ java中怎么用解码读取txt文本文档的内容

要看你的程序类型cs程序 直接使用File类进行操作,File f=new File("文件路径"); 之后构造文件输入流InputStream,在调用read方法读取到byte数组中即可。bs程序 首先要将文件上传到服务器,如servlet或者action中,在使用如上步骤操作即可。

⑨ java怎样设置txt文件的编码格式

可以使用流来修改!百如:String path ="C……"File file = new File(path);Writer outTxt = new OutputStreamWriter(new FileOutputStream(file,true), "UTF-8");outTxt.write("文件内度容格式不正知确, 此文件已被系统删除专!道 ");outTxt.close();注意:记版属事本只支持ANSI、权Unicode、Unicodebigendian、UTF-8四种编码格式。

⑩ Java IO读取txt文件乱码。前提不知道文档是什么编码格式

1首先获得一个文来件句柄。File file = new File(); file即为自文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。2通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西3既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据4解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法读取txt文件中的每一行数据哈。


赞 (0)