『壹』 java :從一個文本文件中讀取所有字元,並輸出。求大神指教
public static void main(String[] args) {
try {
FileReader fileReader = new FileReader("C:/Users/hsh/Desktop/a.wsdl");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String result = null;
while(bufferedReader.ready()){
result += bufferedReader.readLine();
}
System.out.println(result);
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("文件路徑不對,請核對!");
} catch (IOException e) {
System.out.println("輸入過程中意外地到達文件尾或流尾");
}
}
『貳』 java中怎樣將文件的內容讀取成字元串
方式一:
Java code
/**
*以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
*當然也是可以讀字元串的。
*/
/*貌似是說網路環境中比較復雜,每次傳過來的字元是定長的,用這種方式?*/
publicStringreadString1()
{
try
{
//FileInputStream用於讀取諸如圖像數據之類的原始位元組流。要讀取字元流,請考慮使用FileReader。
FileInputStreaminStream=this.openFileInput(FILE_NAME);
ByteArrayOutputStreambos=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlength=-1;
while((length=inStream.read(buffer)!=-1)
{
bos.write(buffer,0,length);
//.write方法SDK的解釋是m.
//當流關閉以後內容依然存在
}
bos.close();
inStream.close();
returnbos.toString();
//為什麼不一次性把buffer得大小取出來呢?為什麼還要寫入到bos中呢?returnnew(buffer,"UTF-8")不更好么?
//returnnewString(bos.toByteArray(),"UTF-8");
}
}
方式二:
Java code

方式四:
Java code
/*InputStreamReader+BufferedReader讀取字元串,InputStreamReader類是從位元組流到字元流的橋梁*/
/*按行讀對於要處理的格式化數據是一種讀取的好方式*/
()
{
intlen=0;
StringBufferstr=newStringBuffer("");
Filefile=newFile(FILE_IN);
try{
FileInputStreamis=newFileInputStream(file);
InputStreamReaderisr=newInputStreamReader(is);
BufferedReaderin=newBufferedReader(isr);
Stringline=null;
while((line=in.readLine())!=null)
{
if(len!=0)//處理換行符的問題
{
str.append(" "+line);
}
else
{
str.append(line);
}
len++;
}
in.close();
is.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnstr.toString();
}
『叄』 java 文件讀寫中哪個方法能夠統計讀取的文件中的字元個數
public static void tongji(String file)throws IOException{
Map< Character, Integer> map=new HashMap<Character, Integer>();
InputStreamReader in=new InputStreamReader(new FileInputStream(file));
int b;
int all=0;
while((b=in.read())!=-1){
char ch=(char)b;
if(b=='\n'||b=='\r'||b==','||b=='。'||b==' '){
continue;
}
Integer n=map.get(ch);
map.put(ch,n==null?1:n+1);
all++;
}
ArrayList<Entry<Character,Integer>> list=new ArrayList<Map.Entry<Character,Integer>>(map.entrySet());
Collections.sort(list,new Comparator<Entry<Character,Integer>>() {
@Override
public int compare(Entry<Character, Integer> o1,
Entry<Character, Integer> o2) {
return o2.getValue()-o1.getValue();
}
});
System.out.println("總字數"+all);
System.out.println("出現的字元數:"+list.size());
for(Entry entry:list){
char ch=(Character) entry.getKey();
int c=(Integer) entry.getValue();
System.out.println(ch+"="+c);
}
}
『肆』 java讀取TXT文件,然後截取字元串
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.Arrays;
importjava.util.Scanner;
publicclass${
publicstaticvoidmain(String[]args){
try{
Scannerin=newScanner(newFile("D:/a.sql"));
while(in.hasNextLine()){
Stringstr=in.nextLine();
String[]arr=str.split(",");
Float[]result=newFloat[arr.length-1];
for(inti=0;i<result.length;i++){
result[i]=Float.parseFloat(trim(arr[i+1]));
}
System.out.println(Arrays.toString(result));
}
in.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
}
privatestaticStringtrim(Stringstr){
str=str.replaceAll("'","");
str=str.replaceAll(";","");
returnstr;
}
}
『伍』 java中如何像readLine()讀取文件一樣讀取字元串
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadFileTest {
public static void main(String[] args) throws IOException {
String temp="";
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(new File("d:/demo.txt")));
byte b[]=new byte[1024*1024];
int length=bis.read(b);
temp=new String(b,0,length);
bis.close();
String str[]=temp.split("\n");
for(int i=0;i<str.length;i++){
System.out.print(str[i]);
}
}
}
已經給樓主做出來了...
不知道是不是樓主想要的,
如果不是樓主想想的話...給我講一下...
我再給樓主修改.....
祝樓主早日成功!
『陸』 JAVA中讀取文件(二進制,字元)內容的幾種方
JAVA中讀取文件內容的方法有很多,比如按位元組讀取文件內容,按字元讀取文件內容,按行讀取文件內容,隨機讀取文件內容等方法,本文就以上方法的具體實現給出代碼,需要的可以直接復制使用
public class ReadFromFile {
/**
* 以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以位元組為單位讀取文件內容,一次讀一個位元組:");
// 一次讀一個位元組
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以位元組為單位讀取文件內容,一次讀多個位元組:");
// 一次讀多個位元組
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字元為單位讀取文件,常用於讀文本,數字等類型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字元為單位讀取文件內容,一次讀一個位元組:");
// 一次讀一個字元
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 對於windows下,\r\n這兩個字元在一起時,表示一個換行。
// 但如果這兩個字元分開顯示時,會換兩次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否則,將會多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字元為單位讀取文件內容,一次讀多個位元組:");
// 一次讀多個字元
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 讀入多個字元到字元數組中,charread為一次讀取字元數
while ((charread = reader.read(tempchars)) != -1) {
// 同樣屏蔽掉\r不顯示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以行為單位讀取文件,常用於讀面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行為單位讀取文件內容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結束
while ((tempString = reader.readLine()) != null) {
// 顯示行號
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 隨機讀取文件內容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("隨機讀取一段文件內容:");
// 打開一個隨機訪問文件流,按只讀方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件長度,位元組數
long fileLength = randomFile.length();
// 讀文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
// 將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次讀10個位元組,如果文件內容不足10個位元組,則讀剩下的位元組。
// 將一次讀取的位元組數賦給byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
/**
* 顯示輸入流中還剩的位元組數
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("當前位元組輸入流中的位元組數為:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
『柒』 java 逐行讀取字元串,是字元串,不是文件。 如: 我們 你 她 他們
/n 。先用這個切分下成數組可以么?
不知道你為什麼要這么做。
『捌』 怎樣用Java讀取txt文件中每行的第9個字元請給出代碼,謝謝~~
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test1 {
public static void main(String[] args) throws IOException{
File file = new File("h:/test.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String str;
while((str = br.readLine()) != null){
char ch = str.charAt(8);
System.out.println(ch);
}
br.close();
}
}
我累累的碼完,發現已經有人寫好了,看起來很專業。我是這兩天剛學到流,順便練習下,我也來學習下高人的代碼!!!
『玖』 java從讀取的文本中 取出指定字元串
這個不難。讀復取文本就當作讀取流的制形式。
具體實現
//讀取一個文本的字元流
BufferedReader in = new BufferedReader(new FileReader("F:\\json.txt"));
String line = null;
//定義一個空字元串來接受讀到的字元串
String str="";
//循環把讀取到的字元賦給str
while((line = in.readLine())!=null)
{
str+=line;
}
System.out.println("str="+str);
//判斷str中是否有EFG子串,為true則說明有。進去if語句
if(str.contains("EFG")){
System.out.println("yes!");
//取得子串的初始位置
int i=str.indexOf("EFG");
//根據的要取的內容後多少字元+多少個
String strEFG=str.substring(i,i+3);
System.out.println("strEFG="+strEFG);
}
『拾』 如何用Java讀取一個txt文件,並將文件內容保存到String類型的變數中求Java代碼!
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();
}
}