當前位置:首頁 » 編程語言 » JAVA讀字元

JAVA讀字元

發布時間: 2023-01-11 16:44:22

1. java中讀取字元和讀取字元串的差別

具體的區別不是很大。先給你看看那API上的介紹。

public class FileInputStream extends InputStream
FileInputStream 從文件系統中的某個文件中獲得輸入位元組。哪些文件可用取決於主機環境。
FileInputStream 用於讀取諸如圖像數據之類的原始位元組流。要讀取字元流,請考慮FileReader。

public class FileReader extends InputStreamReader
用來讀取字元文件的便捷類。此類的構造方法假定默認字元編碼和默認位元組緩沖區大小都是適當的。要自己指定這些值,可以先在 FileInputStream 上構造一個 InputStreamReader。
FileReader 用於讀取字元流。要讀取原始位元組流,請考慮使用 FileInputStream。

public int read()
throws IOException從此輸入流中讀取一個數據位元組。如果沒有輸入可用,則此方法將阻塞。

指定者:
類 InputStream 中的 read
返回:
下一個數據位元組;如果已到達文件末尾,則返回 -1。
拋出:
IOException - 如果發生 I/O 錯誤。

public int read(byte[] b)
throws IOException從此輸入流中將最多 b.length 個位元組的數據讀入一個 byte 數組中。在某些輸入可用之前,此方法將阻塞。

覆蓋:
類 InputStream 中的 read
參數:
b - 存儲讀取數據的緩沖區。
返回:
讀入緩沖區的位元組總數,如果因為已經到達文件末尾而沒有更多的數據,則返回 -1。
拋出:
IOException - 如果發生 I/O 錯誤。
另請參見:
InputStream.read(byte[], int, int)

public int read(byte[] b,
int off,
int len)
throws IOException從此輸入流中將最多 len 個位元組的數據讀入一個 byte 數組中。如果 len 不為 0,則在輸入可用之前,該方法將阻塞;否則,不讀取任何位元組並返回 0。

覆蓋:
類 InputStream 中的 read
參數:
b - 存儲讀取數據的緩沖區。
off - 目標數組 b 中的起始偏移量。
len - 讀取的最大位元組數。
返回:
讀入緩沖區的位元組總數,如果因為已經到達文件末尾而沒有更多的數據,則返回 -1。
拋出:
NullPointerException - 如果 b 為 null。
IndexOutOfBoundsException - 如果 off 為負、len 為負,或者 len 大於 b.length - off
IOException - 如果發生 I/O 錯誤。
另請參見:
InputStream.read()

一下3個方法都是在InputStream中定義的
public abstract int read()
throws IOException從輸入流中讀取數據的下一個位元組。返回 0 到 255 范圍內的 int 位元組值。如果因為已經到達流末尾而沒有可用的位元組,則返回值 -1。在輸入數據可用、檢測到流末尾或者拋出異常前,此方法一直阻塞。
子類必須提供此方法的一個實現。

public int read(byte[] b)
throws IOException從輸入流中讀取一定數量的位元組,並將其存儲在緩沖區數組 b 中。以整數形式返回實際讀取的位元組數。在輸入數據可用、檢測到文件末尾或者拋出異常前,此方法一直阻塞。
如果 b 的長度為 0,則不讀取任何位元組並返回 0;否則,嘗試讀取至少一個位元組。如果因為流位於文件末尾而沒有可用的位元組,則返回值 -1;否則,至少讀取一個位元組並將其存儲在 b 中。

將讀取的第一個位元組存儲在元素 b[0] 中,下一個存儲在 b[1] 中,依次類推。讀取的位元組數最多等於 b 的長度。設 k 為實際讀取的位元組數;這些位元組將存儲在 b[0] 到 b[k-1] 的元素中,不影響 b[k] 到 b[b.length-1] 的元素。

類 InputStream 的 read(b) 方法的效果等同於:

read(b, 0, b.length)
參數:
b - 存儲讀入數據的緩沖區。
返回:
讀入緩沖區的總位元組數;如果因為已經到達流末尾而不再有數據可用,則返回 -1。

public int read(byte[] b,
int off,
int len)
throws IOException將輸入流中最多 len 個數據位元組讀入 byte 數組。嘗試讀取 len 個位元組,但讀取的位元組也可能小於該值。以整數形式返回實際讀取的位元組數。
在輸入數據可用、檢測到流末尾或者拋出異常前,此方法一直阻塞。

如果 len 為 0,則不讀取任何位元組並返回 0;否則,嘗試讀取至少一個位元組。如果因為流位於文件末尾而沒有可用的位元組,則返回值 -1;否則,至少讀取一個位元組並將其存儲在 b 中。

將讀取的第一個位元組存儲在元素 b[off] 中,下一個存儲在 b[off+1] 中,依次類推。讀取的位元組數最多等於 len。設 k 為實際讀取的位元組數;這些位元組將存儲在 b[off] 到 b[off+k-1] 的元素中,不影響 b[off+k] 到 b[off+len-1] 的元素。

在任何情況下,b[0] 到 b[off] 的元素以及 b[off+len] 到 b[b.length-1] 的元素都不會受到影響。

類 InputStream 的 read(b, off, len) 方法重復調用方法 read()。如果第一次這樣的調用導致 IOException,則從對 read(b, off, len) 方法的調用中返回該異常。如果對 read() 的任何後續調用導致 IOException,則捕獲該異常並將其視為到達文件末尾;到達該點時讀取的位元組存儲在 b 中,並返回發生異常之前讀取的位元組數。在已讀取輸入數據 len 的請求數量、檢測到文件結束標記、拋出異常前,此方法的默認實現將一直阻塞。建議子類提供此方法更為有效的實現。

參數:
b - 讀入數據的緩沖區。
off - 數組 b 中將寫入數據的初始偏移量。
len - 要讀取的最大位元組數。
返回:
讀入緩沖區的總位元組數;如果因為已到達流末尾而不再有數據可用,則返回 -1。

總結一下:
FileInputstream.read()讀取的是單個字元,是ASCII碼表所能表示的字元,要是要讀取像有漢字等其他ASCII不能表示的,就要用FileReader,它讀取的是java支持的字元集。
另外,像要是讀取圖片,pdf,psd等這些東西的話,就要用FileInputStream,即文件位元組流。應為存在計算機中的就是二進制,是ASCII表示的。要是讀取有漢字的.txt,.就用FileReader。其實用的較多的是BuffereReader,進行逐行讀取。

2. java字元讀取

InputStreamReader不屬於FileInputStream的子類的對象,要在reader類內部包裝stream類。 in=new InputStreamReader(new FileInputStream("e:\\java\\wen.txt"));

3. 在java中如何讀入一個字元謝謝

import java.util.Scanner;
.......
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
..........
java.util.Scanner包在jdk1.5以上才支持

4. java讀取字元串

public class StringDemo1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="AAAAA,mmmm,111112121,dddd,11111";

String []st=str.split(",");
for(String sc:st){
System.out.println(sc);

}

}
}

5. java怎麼讀取文本文件中的所有字元

可以用文件流FileInputStream的方式讀取,如果文本文件太大了,不建議一次性往內存中讀,那往往會使之溢出。也可以一行行的讀取,用BufferReader讀,具體的實例都可以網路得到的。

6. JAVA讀取字元,讀取一個執行一個代碼怎麼搞

在此提供兩種方法:
1.可以用String的charAt()方法,例子如下:
for(int i=0;i<test.length();i++){
char tmp = test.charAt(i);
//即tmp為test字元串的第i個字元。
System.out.println(tmp);
}

2.也可用String的toCharArray()方法,例子如下:
String test = "test";
char[] tmp = test.toCharArray();
for (int i = 0; i < tmp.length; i++) {
//即tmp為test轉成的字元數組
System.out.println(tmp[i]);
}

7. java 中怎樣實現從鍵盤讀入單個字元

可以通過」Scanner「函數 直接輸入參數的形式,來實現輸入語句,舉例:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("請輸入一個字元:");
String length=input.next();//輸入一個字元
System.out.println("輸入的字元是:"+length);}
}
備註:輸入字元後,通過回車的形式來確認輸入即可。

8. java讀取字元串操作

File fileA = new File("h:\\x.txt");
FileInputStream fis = new FileInputStream(fileA);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] data = new byte[1024 * 1024];
int i = 0;
int j = 0;
while ((i = bis.read()) != -1) {
data[j] = (byte) i;
j++;
}
bis.close();
fis.close();
File fileB = new File("h:\\y.txt");
FileOutputStream fos = new FileOutputStream(fileB);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(data, 0, j);
bos.flush();
fos.close();
bos.close();
以前寫的重一個TXT文件讀取 再寫到另一個TXT文件 希望能幫你

9. java中讀字元的函數代碼!

/**
* @version 1.0
* @author 韓衛召
*
* 多種方式讀文件的內容
* 按位元組讀取文件內容,按字元讀取文件的內容,按行讀取文件的內容,隨即讀取文件的內容
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.ResourceBundle;

public class ReadFromFile {
/**
* 以位元組為單位讀取文件的內容,常用於二進制文件,如聲音,圖象,影象等文件
*
* @param filename
* 文件名
*/
public static void readFileByBytes(java.lang.String filename) {
File file = new File(filename);
InputStream in = null;
System.out.println("以位元組為單位讀取文件的內容,一次讀一個位元組: ");
// 一次讀一個位元組
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int tempbyte;
try {
// 不斷的讀取,直到文件結束
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
System.out.println("以位元組為單位讀取文件內容,一次讀多個位元組: ");
// 一次讀取多個位元組
byte[] tempbytes = new byte[100];
int byteread = 0;
try {
in = new FileInputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ReadFromFile.showAvailabelBytes(in);
try {
while ((byteread = in.read(tempbytes)) != -1) {
// 讀取多個位元組到數組中,byteead為一次讀取的位元組數
System.out.write(tempbytes, 0, byteread);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void readFileByteChars(java.lang.String filename) {
/**
* 以字元為單位讀取文件,常用與讀文本,數字等類型的文件
*/
File file = new File(filename);
Reader reader = null;
System.out.println("以字元為單位讀取文件內容,一次讀一個位元組: ");
// 一次讀一個字元
try {
reader = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int tempchar;
try {
while ((tempchar = reader.read()) != -1) {
// 在Window下,\r\n這兩個字元在一起時,表示一個換行
// 但如果這兩個字元分開顯示時,會換行兩次行
// 因此,屏蔽掉\r,或者\n;否則,將會多出來很多空行
if (((char) tempchar) != '\r') {
System.out.println((char) tempchar);
}
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("以字元為單位讀取文件內容,一次讀多個字元: ");
char[] tempchars = new char[30];
int charread = 0;
try {
reader = new InputStreamReader(new FileInputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 讀入多個字元到字元數組中,charread為一次讀取字元數
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.println(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}

public static void readFileByLines(java.lang.String filename) {
File file = new File(filename);
BufferedReader reader = null;
// System.out.println("以行為單位讀取文件的內容,一次讀一整行: ");
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.lang.String tempString = null;
int line = 1;
try {
while ((tempString = reader.readLine()) != null) {
if(tempString.indexOf("福州")!=-1){
System.out.println("靠!出錯了!趕緊報警啊! " + line + ": " + tempString);

}
// System.out.println("line " + line + ": " + tempString);
line++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} 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();
}
}
}
}

public static void readFileByRandomAccess(java.lang.String filename) {
RandomAccessFile randomFile = null;
System.out.println("隨即讀取一段文件內容: ");
try {
randomFile = new RandomAccessFile(filename, "r");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long fileLength = 0;
try {
fileLength = randomFile.length();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int beginIndex = (fileLength > 4) ? 4 : 0;
try {
randomFile.seek(beginIndex);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bytes = new byte[10];
int byteread = 0;
try {
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

private static void showAvailabelBytes(InputStream in) {
try {
System.out.println("當前位元組輸入流中的位元組數為: " + in.available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(java.lang.String args[]) {
ResourceBundle rb = ResourceBundle.getBundle("PreventTampering");
String filePath = rb.getString("filePath");

java.lang.String filename = filePath+"test.txt";
System.out.println(filename);
// ReadFromFile.readFileByBytes(filename);
// ReadFromFile.readFileByteChars(filename);
ReadFromFile.readFileByLines(filename);
// ReadFromFile.readFileByRandomAccess(filename);
}

10. java中怎樣將文件的內容讀取成字元串

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

}

熱點內容
字體android 發布:2025-07-12 21:30:38 瀏覽:621
資料庫中包含 發布:2025-07-12 21:25:08 瀏覽:621
艦娘緩存系統 發布:2025-07-12 21:21:21 瀏覽:100
cpu對存儲器的讀寫 發布:2025-07-12 21:21:14 瀏覽:772
如何建立一個網站需要伺服器 發布:2025-07-12 21:18:40 瀏覽:67
php登陸微信 發布:2025-07-12 21:17:55 瀏覽:14
公眾伺服器有什麼功能 發布:2025-07-12 21:11:22 瀏覽:715
健身的壓縮衣 發布:2025-07-12 21:11:12 瀏覽:754
磁碟伺服器如何管理磁碟 發布:2025-07-12 21:02:19 瀏覽:470
安卓返回鍵在哪裡取消 發布:2025-07-12 20:50:17 瀏覽:799