【加急送分】java读取txt文件内容,存到二维数组,好手速度来
【加急!送分】java读取txt文件内容,存到二维数组,高手速度来!
如,
jhert.txt
#dsfdsfd 322324 fdsfsdgwq
# wedsd 211211121 dsggjj
#ewhjdd 3236754 wqewewqa
#lkjhopd 1564987 puiouyiuh
#qooybd 454757
fk1 457845 AL 22321 LJKD
fk2 545785 FH wqqw POIOUO
fk3 457892 BV 23560 HGIHGIU
fk4 895267 KA 21287 HIHHKJU
fk5 784525 QW 54578 TYRTYFS
...........
我想把带#号的忽略掉从fk1往下面读存到数组中 并且每一行只读到 第三个就停止
求大神指教,有相关内容代码直接参考更好,答对我把所有分值奉上给你,非常感谢
------解决方案--------------------
public static void main(String[] args) {
try {
String encoding = "GBK";
File file = new File("jhert.txt ");
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if ('#'==lineTxt.charAt(0)) {
continue;
}else{
String[] arr=lineTxt.split(" ");
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + " ");
}
System.out.println();
}
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
------解决方案--------------------
------解决方案--------------------
如,
jhert.txt
#dsfdsfd 322324 fdsfsdgwq
# wedsd 211211121 dsggjj
#ewhjdd 3236754 wqewewqa
#lkjhopd 1564987 puiouyiuh
#qooybd 454757
fk1 457845 AL 22321 LJKD
fk2 545785 FH wqqw POIOUO
fk3 457892 BV 23560 HGIHGIU
fk4 895267 KA 21287 HIHHKJU
fk5 784525 QW 54578 TYRTYFS
...........
我想把带#号的忽略掉从fk1往下面读存到数组中 并且每一行只读到 第三个就停止
求大神指教,有相关内容代码直接参考更好,答对我把所有分值奉上给你,非常感谢
------解决方案--------------------
public static void main(String[] args) {
try {
String encoding = "GBK";
File file = new File("jhert.txt ");
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if ('#'==lineTxt.charAt(0)) {
continue;
}else{
String[] arr=lineTxt.split(" ");
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + " ");
}
System.out.println();
}
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
------解决方案--------------------
package shi;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test13 {
/**
* 读取文件
* @param filePath
* @return
*/
public static List readTxtFile(String filePath) {
List<String> list = new ArrayList<String>();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if (!lineTxt.startsWith("#"))
list.add(lineTxt);
}
read.close();
} else {
System.out.println("找不到文件");
}
} catch (Exception e) {
System.out.println("出错了");
e.printStackTrace();
}
return list;
}
/**
* 创建二维数组
* @param list
* @return
*/
public static String[][] createArray(String filePath){
List<String> list = readTxtFile(filePath);
String array[][] = new String[list.size()][];
for(int i=0;i<list.size();i++){
array[i] = new String[3];
String linetxt=list.get(i);
String[] myArray = linetxt.replaceAll("\\s+", "@").split("@");
for(int j=0;j<myArray.length;j++){
if(j<3){
array[i][j]=myArray[j];
}
}
}
return array;
}
/**
* 打印数组
* @param array
*/
public static void printArray(String array[][]){
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
if(j!=array[i].length-1){
System.out.print("array["+i+"]["+j+"]="+array[i][j]+",");
}
else{
System.out.print("array["+i+"]["+j+"]="+array[i][j]);
}
}
System.out.println();
}
}
public static void main(String args[]) {
String array[][] = createArray("F:\\test1.txt");
printArray(array);
}
}
------解决方案--------------------
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("emplyee.txt"))));