解决方案 »
- 我想学习下J2EE,我是新手。一点不会。大家来讨论下
- 关于jfreechart生成的图表和其他表格共同存在同一页面的问题....
- ----------有网站的兄弟们进来!顶者有分--------------
- struts spring 整合需要引入什么必要的包
- 帮帮忙,为什么我的structs2 jquery ajax去做登录可是后台接收不到数据,这是关于毕业设计的,时间紧迫
- xsl报文求解
- 链接到某一jsp页面,就强制刷新一次,请问如何实现啊(赚分了的小问题!)
- 郁闷,servlet内调用bean(操作数据库)出错+websphere+200分
- 十万火急,请高人来帮忙啊!!!!!!100分马上送出!!!!
- 为什么不能通过配置iis来访问jsp?
- 用自己主机架设一个web服务器,别人为什么访问不了???
- java 网站发布
public class Test {
public static void main(String[] args) throws Throwable {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("map01.txt")));
String buf = "";
List<List<Integer>> list = new ArrayList<List<Integer>>();
while ((buf=br.readLine()) != null) {
if (! buf.matches("(\\d+(\\s+|$)){3,}")) continue;
String[] sa = buf.split("\\s+");
int x = Integer.valueOf(sa[0]);
int y = Integer.valueOf(sa[1]);
int z = Integer.valueOf(sa[2]);
while (list.size() < y) {
list.add(new ArrayList<Integer>());
}
List<Integer> sub = list.get(y-1);
while (sub.size() < x) {
sub.add(0);
}
sub.set(x-1, z);
} System.out.printf(" ");
for (int i=0; i<12; i++) {
System.out.printf("%02d ", i+1);
}
System.out.println(); for (int i=0; i<8; i++) {
System.out.printf("%03d||", i+1);
if (i<list.size()) {
List<Integer> sub = list.get(i);
for (int j : sub) {
if (j == 0) {
System.out.printf(" ");
} else {
System.out.printf("%02d ", j);
}
}
}
System.out.println();
}
}
}
java Test > result.txt
这样就可以把控制台的信息输出到result文件了在代码中直接输出文件也很简单,用一个打印流打印到文件,即
PrintStream ps = new PrintStream("result.txt");
然后原来的代码的System.out的地方用ps来代替就可以了.代替空值就更简单了,在原来的代码的空值判断的地方,把打印空格改成打印.就可以了
至于坐标,就是代码的循环的地方修改一下就可以了修改后代码
import java.util.*;
public class Test {
public static void main(String[] args) throws Throwable {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("map01.txt")));
PrintStream ps = new PrintStream("result.txt"); //用ps代替System.out就可以了
String buf = "";
List<List<Integer>> list = new ArrayList<List<Integer>>();
while ((buf=br.readLine()) != null) {
if (! buf.matches("(\\d+(\\s+|$)){3,}")) continue;
String[] sa = buf.split("\\s+");
int x = Integer.valueOf(sa[0]);
int y = Integer.valueOf(sa[1]);
int z = Integer.valueOf(sa[2]);
while (list.size() < y) {
list.add(new ArrayList<Integer>());
}
List<Integer> sub = list.get(y-1);
while (sub.size() < x) {
sub.add(0);
}
sub.set(x-1, z);
} ps.printf(" "); //ps代替System.out
for (int i=0; i<185; i++) { //改成最大x坐标为185
ps.printf("%02d ", i+1); //ps代替System.out
}
ps.println(); //ps代替System.out for (int i=0; i<9; i++) { //改成最大y坐标为9
ps.printf("%03d||", i+1); //ps代替System.out
if (i<list.size()) {
List<Integer> sub = list.get(i);
for (int j=0; j<185; j++) {//这里改成到185为止循环
if (j < sub.size()) { //如果有数组,则打印数据
ps.printf("%02d ", sub.get(j)); //ps代替System.out
} else { //没数据则打印空值
ps.printf(". "); //用.代替空值,ps代替System.out
if (j > 99) {ps.print(" ");} //考虑到3位数以后的对齐,调整空格数
}
}
}
ps.println(); //ps代替System.out
}
br.close();
ps.close(); //关闭文件
}
}
import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Throwable {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("map01.txt")));
PrintStream ps = new PrintStream("result.txt"); //用ps代替System.out就可以了
String buf = "";
List<List<Integer>> list = new ArrayList<List<Integer>>();
while ((buf=br.readLine()) != null) {
if (! buf.matches("(\\d+(\\s+|$)){3,}")) continue;
String[] sa = buf.split("\\s+");
int x = Integer.valueOf(sa[0]);
int y = Integer.valueOf(sa[1]);
int z = Integer.valueOf(sa[2]);
while (list.size() < y) {
list.add(new ArrayList<Integer>());
}
List<Integer> sub = list.get(y-1);
while (sub.size() < x) {
sub.add(0);
}
sub.set(x-1, z);
} ps.printf(" "); //ps代替System.out
for (int i=0; i<185; i++) { //改成最大x坐标为185
ps.printf("%02d ", i+1); //ps代替System.out
}
ps.println(); //ps代替System.out for (int i=0; i<9; i++) { //改成最大y坐标为9
ps.printf("%03d||", i+1); //ps代替System.out
if (i<list.size()) {
List<Integer> sub = list.get(i);
for (int j=0; j<185; j++) {
if (j < sub.size()) {
ps.printf("%02d ", sub.get(j)); //ps代替System.out
} else {
ps.printf(". "); //用.代替空值,ps代替System.out
if (j > 99) {ps.print(" ");}
}
}
} else { //这里遗漏了,重新修改一下
for (int j=0; j<185; j++) {
ps.printf(". "); //用.代替空值,ps代替System.out
if (j > 99) {ps.print(" ");}
}
}
ps.println(); //ps代替System.out
}
br.close();
ps.close(); //关闭文件
}
}
LZ你也太依赖别人了吧,要学会自己动手才能长进啊import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws Throwable {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("map01.txt")));
PrintStream ps = new PrintStream("result.txt");
String buf = "";
List<List<Integer>> list = new ArrayList<List<Integer>>();
while ((buf=br.readLine()) != null) {
if (! buf.matches("(\\d+(\\s+|$)){3,}")) continue;
String[] sa = buf.split("\\s+");
int x = Integer.valueOf(sa[0]);
int y = Integer.valueOf(sa[1]);
int z = Integer.valueOf(sa[2]);
while (list.size() < y) {
list.add(new ArrayList<Integer>());
}
List<Integer> sub = list.get(y-1);
while (sub.size() < x) {
sub.add(0);
}
sub.set(x-1, z);
} ps.printf(" ");
for (int i=0; i<185; i++) {
ps.printf("%02d ", i+1);
}
ps.println(); for (int i=0; i<9; i++) {
ps.printf("%03d||", i+1);
if (i<list.size()) {
List<Integer> sub = list.get(i);
for (int j=0; j<185; j++) {
if (j < sub.size()) {
ps.printf("%02d ", sub.get(j));
} else {
ps.print("000. ".substring((j+1>99 ? 1 : 2))); //修改这里就可以
}
}
} else {
for (int j=0; j<185; j++) {
ps.print("000. ".substring((j+1>99 ? 1 : 2))); //修改这里就可以了
}
}
ps.println();
}
br.close();
ps.close();
}
}
这什么意思 ? matches 里面的
+ 表示出现一次或多次
\\s是空白符号(也就是空格符,tab符,换行符等等)
$ 表示字符串结束符号
{3,} 表示出现3次以上这样可以保证文件有非法数据的时候不出现异常而继续执行