第一周学习Java。我的破碎的代码在下面道歉。试图打开一个文件读取它并选择需要的内容写入另一个文件。我的文件是这样的:如何打开并读取文件并将特定内容写入新文件?
DESCRIPTION:
TITILE:
TYPE: image
SOURCE: Library
FORMAT: jpeg
IDENTIFIER: 120034
LATITUDE: 40.109580
LONGITUDE: -88.228378
DESCRIPTION:
TITLE: GSLIS
SUBJECT:
TYPE: image
SOURCE: Library
FORMAT: jpeg
IDENTIFIER: 120155
LATITUDE: 40.107779
LONGITUDE:-88.231621
我只是写两段代码,一个用于打开和读取,一个用于匹配模式:
package Functions;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class readFileLocal {
private static final String[] String = null;
private String path;
public readFileLocal(String file_path){
path = file_path;
}
public String[] openFile() throws IOException{
FileReader freader = new FileReader (path);
BufferedReader textReader = new BufferedReader (freader);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i<numberOfLines; i++){
String newLine=new String();
newLine=null;
textData[i] = textReader.readLine();
String a = textData.toString();
while ((textData[i])!=null){
if (a.startsWith("Identifier: ") || a.startsWith("Latitude: ")||a.startsWith("Longitude:")){
newLine.append(a);
}
boolean filled1 =Boolean.valueOf(String newLine[0]);
boolean filled2 =Boolean.valueOf(String newLine[1]);
boolean filled3 =Boolean.valueOf(String newLine[2]);
if(filled1, filled2, filled3) {
System.out.println(newLine[0]+'|'+newLine[1]+"|"+newLine[2]+"\n");
}
}
}
textReader.close();
}
int readLines() throws IOException{
FileReader file_to_read = new FileReader(path);
BufferedReader lines = new BufferedReader (file_to_read);
String aLine=lines.readLine();
int numberOfLines = 0;
while(aLine != null) {
numberOfLines ++;
}
lines.close();
return numberOfLines;
}
}
我也想通了如何搜索字符串中我想与人的帮助的方式,如下图所示:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadLatLong
{
public ReadLatLong(){
String line = "IDENTIFIER: 115956 LATITUDE: 40.104730 LONGITUDE: -88.228798 DATE RIGHTS IDENTIFIER: 115956 LATITUDE: 40.104730 LONGITUDE: -88.228798 DATE RIGHTS";
String pattern = "IDENTIFIER:\\s(\\d*)\\sLATITUDE:\\s(\\d*\\.?\\d*)\\sLONGITUDE:\\s(.*?)\\s";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println("Image: " + m.group(1)+"|"+m.group(2)+"|"+m.group(3));
}
}
}
现在我不知道如何搜索到è整个文件抓住所有标识符,纬度,经度,并把他们全部是这样的: 120034 | 40.109580 | -88.228378 \n
120155 | 40.107779 | -88.231621 \n
这是我的主要方法,但我不知道如何把它写在一个新的文件无论是。
package readText;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FileData {
public static void main(String[] args) throws Exception {
String file_path1 = "F:\\CampusImages.txt";
String file_path2 = "F:\\CampusImageIDs.txt";
try{
ReadFileLocal file = new ReadFileLocal(file_path1);
WriteFile writeout = new WriteFile(file_path2); //Don't know how to write it in new file.
PrintWriter writer = new PrintWriter(new FileWriter(new File(file_path2)));
String[] arylines = file.openFile();
writeout.WriteToFile(String.valueOf(arylines));
int i;
for (i=0; i<arylines.length; i++){
System.out.println(arylines[i]);
}
writer.close();
}
catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
在此先感谢。
不,这不是我的家庭作业。这只是我试图做的一个项目,同时也是在挑选Java。我在学校学了一个小蟒蛇,那是我用来理解Java的镜头,但我的Python也很基础。 :-P无论如何,谢谢你的线索。 – qximin 2015-03-19 03:04:25
对不起,我使用的不适当的措辞。最近发生了不合理的事情。我回想起当我开始使用Java时,曾经在其他人使用javadoc发送给我的示例代码中查看每个单词。如果你有时间,你可以考虑在javadoc中查询语句及其含义。这对我的情况有很大的帮助。 – senderj 2015-03-19 05:55:02
谢谢。我正在阅读不同的资源。我喜欢用于学习的是其他人的代码。我喜欢阅读定义,示例代码和详细解释,以便我能更好地理解事物。 – qximin 2015-03-19 14:46:26