如何读取二维坐标轴上的数据到txt文件里 本帖最后由 areyougay 于 2012-04-26 14:54:29 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上面的二维坐标轴上的数据也是一个txt文件。读取这个txt文件,转换成6 1 47 1 18 1 46 2 17 2 18 2 1.......这种形式的result.txt文件 理解了,不算太复杂,如果你只是要算法思路的话,大致这样就行了:用FileWriter打开目标文件(输出结果用的);用Scanner配合FileReader打开源数据文件,然后逐行读取(第一行不用管了,反正没啥用);然后针对每一行:001||..........040104..........可以发现:前5个字符是没意义的(行号可以自己算,当然你也可以取前三个字符然后转为数字)。实际上就是对该行进行逐个字符的遍历,类似: for (int i=5;i<line.length();i++)判断字符是否不等于 句号,类似: line.chatAt(i) != '.'如果不等于句话,就用FileWriter输出:i-4 行号 line.chatAt(i)自己先写写看,搞不定,再把代码贴上来讨论。 import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class test5 { public static void main(String[] args) throws IOException { FileWriter fw=null; BufferedReader br =null; try { br= new BufferedReader(new FileReader("D://习题//習題1//源文件//座標檔2.txt")); fw = new FileWriter("d://result5.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String buf =""; try { for(int j=0;j<105;j++){ while ((buf=br.readLine())!= null){ for(int i=6;i<buf.length();i++){ if(buf.charAt(i)!='.'){ fw.write(buf); System.out.println(); }} } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }为什么分不了行,上面说错了竖行是x轴,横行是y轴预期结果是:(最后一列不要管)1 6 4 51 7 1 51 8 4 12 6 1 62 7 1 52 8 1 13 6 1 73 7 1 63 8 1 24 6 1 84 7 1 74 8 1 35 5 1 55 6 1 15 7 1 8...... 没看见你分行了。另外你的循环逻辑错误;输出也少了很多东西。int row = 1;while ((buf=br.readLine())!= null){ for(int i=5;i<buf.length();i++){ if(buf.charAt(i)!='.'){ String tmp = row + " " + (i-4) + " " + buf.charAt(i) + "\n"; fw.write(tmp); System.out.print(tmp); } } row++;} 读文件时要从第二行开始读取,因为第一行是y坐标用charat 读取的是一个字节,但是二维坐标上的数据10以下是2位数 比如7写的是07,所以要读两位。怎么修改呢? “读文件时要从第二行开始读取,因为第一行是y坐标”那么在循环开始之前,先用一个readLine()跳过第一行。“用charat 读取的是一个字节,但是二维坐标上的数据10以下是2位数 比如7写的是07,所以要读两位。怎么修改呢?”那个坐标其实没啥意义,而且我看你后面的那些信息并没有跟坐标对齐,是紧凑型的,所以不管那个坐标就行了。 我解决了问题谢谢了。现在想要反过来,将:1 6 4 51 7 1 51 8 4 12 6 1 62 7 1 5......这里面的input.txt的数据写在下面的二维坐标轴上output.txt上,可以截取每行数据写入数组,但是如何将z定位在坐标轴上呢?不要太难的代码 01020304050607080910111213001||..........040104..........002||..........010101..........003||..........010101..........004||..........010101..........005||........0101010101........006||........0101010101........007||........0101010101........008||........0101010101........009||......01010101010101......import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.PrintStream;public class test6 { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br =null; PrintStream ps=null; try { br= new BufferedReader(new FileReader("D://input.txt")); ps=new PrintStream("d://output.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String buf=""; while ((buf=br.readLine())!= null) { String[] sa = buf.split("\\s+"); int x = Integer.valueOf(sa[0]); int y = Integer.valueOf(sa[1]); int z = Integer.valueOf(sa[2]); } ps.printf(" "); for (int i=0; i<12; i++) { ps.printf("%02d", i+1); } ps.println(); for (int i=0; i<190; i++) { ps.printf("%03d||", i+1); ps.println(); } } } 数据库从sql2005换到sql2008,sqljdbc4.jar 出现异常, HQL语句出错 求一个JNI调用VS2010生成的dll的例子 在线等待 关于ssh2+jquery swt设选项卡间隔问题 关于window.open()请求servlet的问题 请教一个spring的配置问题,对高手来说应该算是简单的了!! 在java中如何防止二重启动,相当于VB中函数App.PrevInstance的功能 Spring MVC jquery upload控件跳转问题 JBOSS启动报错 求救struts iterator 求高手解决div布局问题
6 1 4
7 1 1
8 1 4
6 2 1
7 2 1
8 2 1
.......这种形式的result.txt文件
用Scanner配合FileReader打开源数据文件,然后逐行读取(第一行不用管了,反正没啥用);
然后针对每一行:
001||..........040104..........
可以发现:前5个字符是没意义的(行号可以自己算,当然你也可以取前三个字符然后转为数字)。
实际上就是对该行进行逐个字符的遍历,类似: for (int i=5;i<line.length();i++)
判断字符是否不等于 句号,类似: line.chatAt(i) != '.'
如果不等于句话,就用FileWriter输出:i-4 行号 line.chatAt(i)自己先写写看,搞不定,再把代码贴上来讨论。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class test5 {
public static void main(String[] args) throws IOException {
FileWriter fw=null;
BufferedReader br =null; try {
br= new BufferedReader(new FileReader("D://习题//習題1//源文件//座標檔2.txt"));
fw = new FileWriter("d://result5.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String buf ="";
try {
for(int j=0;j<105;j++){
while ((buf=br.readLine())!= null){
for(int i=6;i<buf.length();i++){
if(buf.charAt(i)!='.'){
fw.write(buf);
System.out.println();
}}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
为什么分不了行,上面说错了竖行是x轴,横行是y轴
预期结果是:(最后一列不要管)
1 6 4 5
1 7 1 5
1 8 4 1
2 6 1 6
2 7 1 5
2 8 1 1
3 6 1 7
3 7 1 6
3 8 1 2
4 6 1 8
4 7 1 7
4 8 1 3
5 5 1 5
5 6 1 1
5 7 1 8
......
另外你的循环逻辑错误;输出也少了很多东西。int row = 1;
while ((buf=br.readLine())!= null){
for(int i=5;i<buf.length();i++){
if(buf.charAt(i)!='.'){
String tmp = row + " " + (i-4) + " " + buf.charAt(i) + "\n";
fw.write(tmp);
System.out.print(tmp);
}
}
row++;
}
怎么修改呢?
那么在循环开始之前,先用一个readLine()跳过第一行。“用charat 读取的是一个字节,但是二维坐标上的数据10以下是2位数 比如7写的是07,所以要读两位。
怎么修改呢?”
那个坐标其实没啥意义,而且我看你后面的那些信息并没有跟坐标对齐,是紧凑型的,所以不管那个坐标就行了。
1 6 4 5
1 7 1 5
1 8 4 1
2 6 1 6
2 7 1 5
......
这里面的input.txt的数据写在下面的二维坐标轴上output.txt上,可以截取每行数据写入数组,但是如何将z定位在坐标轴上呢?不要太难的代码 01020304050607080910111213
001||..........040104..........
002||..........010101..........
003||..........010101..........
004||..........010101..........
005||........0101010101........
006||........0101010101........
007||........0101010101........
008||........0101010101........
009||......01010101010101......import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
public class test6 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br =null;
PrintStream ps=null;
try {
br= new BufferedReader(new FileReader("D://input.txt"));
ps=new PrintStream("d://output.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String buf="";
while ((buf=br.readLine())!= null) {
String[] sa = buf.split("\\s+");
int x = Integer.valueOf(sa[0]);
int y = Integer.valueOf(sa[1]);
int z = Integer.valueOf(sa[2]);
} ps.printf(" ");
for (int i=0; i<12; i++) {
ps.printf("%02d", i+1);
}
ps.println(); for (int i=0; i<190; i++) {
ps.printf("%03d||", i+1);
ps.println();
}
}
}