[英]How can I read characters until a specific one in Java?
我想從文件中讀取幾句話。 我沒有找到執行此操作的任何方法,所以我決定逐個讀取char ,但是我需要停在將讀取的單詞存儲在數組中的空格處,然后轉到下一個。
我正在進行外部排序應用程序,這就是為什么我有內存限制的原因,在那種情況下,我不能只使用readLine()
然后再split()
,我需要控制自己的讀物。
read()
方法返回一個int ,我不知道我該怎么辦read()
方法返回一個char並在空格后停止讀取。
到目前為止,這是我的代碼:
protected static String [] readWords(String arqName, int amountOfWords) throws IOException {
FileReader arq = new FileReader(arqName);
BufferedReader lerArq = new BufferedReader(arq);
String[] words = new String[amountOfWords];
for (int i = 0; i < amountOfWords; i++){
//words[i] = lerArq.read();
}
return words;
}
編輯1:我使用了Scanner和next()
方法,它起作用了。 掃描儀的初始化位於Main。
static String [] readWords(int amountOfWords, Scanner leitor) throws IOException {
String[] words= new String[amountOfWords];
for (int i = 0; i < amountOfWords; i++){
words[i] = leitor.next();
}
return words;
}
也許這會有所幫助。
使用read()
沒問題。 只需將結果轉換為字符即可:
...
for (int i = 0; i < memTam; i++) {
// this should work. you will get the actual character
int current = lerArq.read();
if (current != -1) {
char c = (char) current;
// then you can do what you need with this character
}
}
...
該方法返回讀取的字符,為0到65535之間的整數,如果已到達流的末尾,則返回-1。
我不會添加很多有關編碼,如何在Java中完成編碼的理論,因為我不了解一些非常底層的細節。 我對它的工作原理有基本的了解。
鍵盤上的每個鍵都有一個與之關聯的數字。 您鍵入的每個字符都可以轉換為十進制數字。 例如, A
變為數字65
。 這是一個標准,已得到全球認可。
在這一點上,我希望你可以同意, read()
方法返回一個數字而不是實際的字符不是很奇怪:)
有一個叫做ASCII表的東西,它代表鍵盤上所有鍵的所有那些代碼(數字)。
這只是顯示ot的外觀:
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
因此,假設您有一個帶有一些文本的.txt
文件-所有字母都有相應的數字。
ASCII的問題在於ASCII定義了128個字符,這些字符映射到數字0–127(所有大寫字母,小寫字母,0-9數字和更多的符號)。
但是世界上還有更多不同的字符/符號(不同的字母,表情符號等),因此必須有另一種編碼系統來表示它們。
它稱為Unicode。 對於代碼為0-127的字符,Unicode完全相同。 但是總的來說,Unicode可以代表更廣泛的符號。
在Java中, char
數據類型(以及因此Character
對象封裝的值)基於原始Unicode規范,該規范將字符定義為固定寬度的16位實體。 您可以在此javadoc中查看更多詳細信息。 換句話說,Java中的所有字符串都以UTF-16表示。
希望在這段漫長的故事之后,在某種意義上為什么您在閱讀時會得到數字是有道理的,但是您可以將其轉換為char
類型。 同樣,這只是一種高級概述。 快樂編碼:)
如果您想逐個字符地讀取它(這樣您就可以更好地控制要存儲的內容和不需要的內容),可以嘗試如下操作:
import java.io.BufferedReader;
import java.io.IOException;
[...]
public static String readNextWord(BufferedReader reader) throws IOException {
StringBuilder builder = new StringBuilder();
int currentData;
do {
currentData = reader.read();
if(currentData < 0) {
if(builder.length() == 0) {
return null;
}
else {
return builder.toString();
}
}
else if(currentData != ' ') {
/* Since you're talking about words, here you can apply
* a filter to ignore chars like ',', '.', '\n', etc. */
builder.append((char) currentData);
}
} while (currentData != ' ' || builder.length() == 0);
return builder.toString();
}
然后這樣稱呼它:
String[] words = new String[amountOfWordsToRead];
for (int i = 0; i < amountOfWordsToRead; i++){
words [i] = readNextWord(yourBufferedReader);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.