在线等...紧急求救!java如何将控制台的文件写入txt文本,怎样将以下输出的代码保存到txt文本中??? 5
importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.i...
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.Random;
public class mi {
private char map[][];
private Random rand = new Random();
private final int x = 10, y = 22;
public mi() {
map = new char[50][48];
}
public void printMaze() {
int z1, z2,t;
for (z2 = 1; z2 <= x * 2 + 1; z2++) {
for (z1 = 1; z1 <= y * 2 + 1; z1++) {
if (map[z2][z1] == 0) {
if ( z1==1 )
{
System.out.print("*");
}
else
{
System.out.print("0");
}
} else {
t=z2+z1;
if ( t>=66 )
{
System.out.print("#");
}
else
{
System.out.print("1");
}
}
}
System.out.println();
}
System.out.println();
}
public void makeMaze() {
rand.setSeed(System.currentTimeMillis());
int z1, z2;
for (int i = 0; i <= x * 2 + 2; ++i)
for (int j = 0; j <= y * 2 + 2; ++j)
map[i][j] = 1;
for (z1 = 0, z2 = 2 * y + 2; z1 <= 2 * x + 2; ++z1) {
map[z1][0] = 0;
map[z1][z2] = 0;
}
for (z1 = 0, z2 = 2 * x + 2; z1 <= 2 * y + 2; ++z1) {
map[0][z1] = 0;
map[z2][z1] = 0;
}
map[2][1] = 0;
map[2 * x][2 * y + 1] = 0;
searchPath(rand() % x + 1, rand() % y + 1);
}
private int searchPath(int x, int y) {
int dir[][] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
int zx = x * 2;
int zy = y * 2;
int next, turn, i;
map[zx][zy] = 0;
turn = 3;
if (rand() % 2 == 1)
turn = 1;
for (i = 0, next = rand() % 4; i < 4; ++i, next = (next + turn) % 4) {
if (map[zx + 2 * dir[next][0]][zy + 2 * dir[next][1]] == 1) {
map[zx + dir[next][0]][zy + dir[next][1]] = 0;
searchPath(x + dir[next][0], y + dir[next][1]);
}
}
return 0;
}
private int rand() {
return Math.abs(rand.nextInt());
}
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
mi m = new mi();
m.makeMaze();
m.printMaze();
}
} 展开
import java.io.File;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.Random;
public class mi {
private char map[][];
private Random rand = new Random();
private final int x = 10, y = 22;
public mi() {
map = new char[50][48];
}
public void printMaze() {
int z1, z2,t;
for (z2 = 1; z2 <= x * 2 + 1; z2++) {
for (z1 = 1; z1 <= y * 2 + 1; z1++) {
if (map[z2][z1] == 0) {
if ( z1==1 )
{
System.out.print("*");
}
else
{
System.out.print("0");
}
} else {
t=z2+z1;
if ( t>=66 )
{
System.out.print("#");
}
else
{
System.out.print("1");
}
}
}
System.out.println();
}
System.out.println();
}
public void makeMaze() {
rand.setSeed(System.currentTimeMillis());
int z1, z2;
for (int i = 0; i <= x * 2 + 2; ++i)
for (int j = 0; j <= y * 2 + 2; ++j)
map[i][j] = 1;
for (z1 = 0, z2 = 2 * y + 2; z1 <= 2 * x + 2; ++z1) {
map[z1][0] = 0;
map[z1][z2] = 0;
}
for (z1 = 0, z2 = 2 * x + 2; z1 <= 2 * y + 2; ++z1) {
map[0][z1] = 0;
map[z2][z1] = 0;
}
map[2][1] = 0;
map[2 * x][2 * y + 1] = 0;
searchPath(rand() % x + 1, rand() % y + 1);
}
private int searchPath(int x, int y) {
int dir[][] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
int zx = x * 2;
int zy = y * 2;
int next, turn, i;
map[zx][zy] = 0;
turn = 3;
if (rand() % 2 == 1)
turn = 1;
for (i = 0, next = rand() % 4; i < 4; ++i, next = (next + turn) % 4) {
if (map[zx + 2 * dir[next][0]][zy + 2 * dir[next][1]] == 1) {
map[zx + dir[next][0]][zy + dir[next][1]] = 0;
searchPath(x + dir[next][0], y + dir[next][1]);
}
}
return 0;
}
private int rand() {
return Math.abs(rand.nextInt());
}
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
mi m = new mi();
m.makeMaze();
m.printMaze();
}
} 展开
1个回答
展开全部
void saveToFile(String path, String name, String con)
{
if(!name.endsWith(".txt"))
name = name + ".txt";
File file = new File(path + File.separator + name);
FileOutputStream fos = null;
try
{
if(!file.exists())
{
file.createNewFile();
}
fos = new FileOutputStream(file);
fos.write(con.getBytes());
} catch (Exception e)
{
System.out.println("文件出错");
}
finally
{
try
{
fos.flush();
fos.close();
} catch (IOException e)
{
}
}
}
{
if(!name.endsWith(".txt"))
name = name + ".txt";
File file = new File(path + File.separator + name);
FileOutputStream fos = null;
try
{
if(!file.exists())
{
file.createNewFile();
}
fos = new FileOutputStream(file);
fos.write(con.getBytes());
} catch (Exception e)
{
System.out.println("文件出错");
}
finally
{
try
{
fos.flush();
fos.close();
} catch (IOException e)
{
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询