我想读取一个文本文件(包含字符)并将它们存储在java中的数组(最好是String或char数组)中。
我有以下文件:
TIRQHHVZZRDAPOBVPSUJIODEZMYPYNGQIODWBXKCTOTRGZZEMP
MLWLOPBIEWVOGGODRFVRZNATTNOYMMQGAWLYYEPZTZLKDZNNOV
YQBSITHGIEQLQOILXJZRMZOAZPUKBBHEZEWRIOXKQITJQYJWYA
WRMHAKAEDTERKFXPNQSHSUOOTCCQDNNSTUNICGHKPSAEBVZCEK
GLHKDMIXJVBGKGDURNOKKDTUVYKKFJIPUCCIKQLUTGXUBVDDWE
IMLOXJTMXFBTOPIHAXCMLDSVOYKJVKPNITIFNHLSBUSKOQYEVE
FIMTDKSLPLOGDPSQNRYKNZWESINHKUAUAJNLQRBESGDIJAZXCZ
XPASOKPBHLESOVQNWHWCJQVKQPAVQWORDJLDEQTZLWVRVCECQE
VSRWFOURTEENPCSZYKJXKDSYMZYFBNNUMKPTPYUYESFMJPQWAX
OFTMDZFHVAGAXAVIMCDFFTYTMBZJXGSTHTWBFKOFMCPMYSDBHW
ZTTDOSGXMRHCWWJZTGRVYZBYAUWGADVBMNPGXISWWQOGYCOJQI
WKCLDAPLTYUACEWYDPFKENVEBQUCRILMZMOTYTZASQZHDKJCUD
YLNPTATVKWKQCKXDOLOTTINLECNSFQAYWXKVGQPUTOCTQRHPLD
KVIDVLMOLCWQOCPIIUMEFEKVLBRJBOOANJVAGENXLDJSZHQEZO
WVMXYCURWDEWZMYQWNDJEWSAEGKXUJSNVDQNYPLGASVJPJTTTK
QYHETHVZXHXLXOFLOYEERHTRVUZUGTTVXSDXGFPLFNJKYEGITB
EOEALPUNSRSAVAMRFKADVNLUEKWPHINLQIRFADMPHBLRABBDYD
WJCFVKAJXQAJGGFTVMHEHEIQNOQVNYCKBETIDJFWGAFHETDFLW
UHJLWHNZRBYPBFUZLDYZRHDZRWUHIOTNXGHTJWUPOWMOLUAJPN
UQEVRGNDRVLCDCZVMUJKXEJESRJWCWPHLSFJRSNZHSIQFCXJC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ZRVDVSZYSSVHEQRQOYUUTAAQEZFBQTCRVTLXVJWHWQSKNJUD
WAZETCDRIUDDQYOKPMFYIVCQJUWVPTYWXKIRJJBEOAXJUAZUSI
GYSWSAWRLXIFICREJUESOBJBYCTSTAXMMMIUVMDTJMZQBUAGTX
XIELYZYXIEDZUULIAAMZMRTEVCUDQRQOYEXPDQUZHCUCOFBUJH
KJYUWVRLIRFPJJTWWHHEUQYZKJBNAUEUOFOSNJQPVGBCJDAZVN
NYMKHEVUWTSONMYOLSSOYHYLHCZJBTLEVSOYMCAYDALDYSMYZJ
BNKEQXFQNLEUBLTEJPZXCHYSRLHYVCKGLHTJBTJUNNHCXEIXNW
VUYPYWZIEVXDRTCMJZQZENNWQPGCQODHEGXQCNJRVYZUSTPYYQ
EAVVSGHIROLJFPRAFPBJTAWYYTLUTTYNJPZBVLSJUNYBFHEPED
我正在使用以下代码,但它不起作用:
public class A3 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileReader f = new FileReader("C:\\Users\\Dell\\Desktop\\COSC600\\sampleinp.txt");
BufferedReader br = new BufferedReader(f);
String ar[][] = new String [50][50];
for(int i=0;i<ar.length;i++) {
for(int j=0;j<ar.length;j++) {
String s = br.readLine();
ar[i][j] = s.split(" ");
}
}
}
}
PS:请忽略进口&amp;代码错误。这只是部分代码。
非常感谢任何帮助!
预期输出:采用二维数组
T I R Q H H V Z Z R D A P O B .........P
M L W L O P B I E W V O G G O .........V
Y Q S I T H G I E Q O .................A
答案 0 :(得分:0)
函数$this->input->post('wktu_up', TRUE);
返回split()
的数组,而不是String
。请尝试以下代码:
String
答案 1 :(得分:0)
你真的需要使用数组吗?我会做这样的事情来读取文件
import java.util.ArrayList;
import java.util.List;
public class A3 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileReader f = new FileReader("C:\\Users\\Dell\\Desktop\\COSC600\\sampleinp.txt");
BufferedReader br = new BufferedReader(f);
List<String[]> words = new ArrayList<String[]>();
line = br.readLine();
while(line != null){
words.add(line.split(""));
line = br.readLine();
}}}