java速度入四.文件的基本操作及猜数字游戏的实现
2011-04-30 11:54
716 查看
1.文件操作。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
public class fileOperator {
private static String filePath="txt.txt";
private static File file=new File(filePath);
private static int runCount=0;
private static String date=String.format("%tF %<tT", new Date());
private static String os=System.getProperty("os.name");
private static String dataStr="";
public static void main(String args[])
{
loadConfig();
if(dataStr.isEmpty()) System.out.println("First run!");
else
{
System.out.println("runCount: "+runCount+" date:"+date+" os: "+os);
}
putConfig() ;
System.out.println("Hello,java!");
}
private static void loadConfig()
{
try
{
if(!file.exists()) file.createNewFile();
byte[] data=new byte[64];
FileInputStream fis=new FileInputStream(file);
int rs=0;
while((rs=fis.read(data))>0) dataStr+=new String(data,0,rs);
if(!dataStr.isEmpty())
{
String[] sets=dataStr.split(",");
runCount=Integer.parseInt(sets[0]);
date=sets[1];
os=sets[2];
}
fis.close();
}catch(Exception e)
{e.printStackTrace();}
}
private static void putConfig()
{
//String dataStr;
try
{
if(!file.exists()) file.createNewFile();
dataStr=(runCount+1)+","+date+","+os;
byte[] data=dataStr.getBytes();
FileOutputStream fout=new FileOutputStream(file);
fout.write(data);
fout.flush();
fout.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
一个简单的文件操作,将系统信息写入到一个文本文件。
2.猜数字游戏
import java.util.Scanner;
public class guessNumber {
public static void main(String[]args)
{
int ans=(int)(Math.random()*10);
int num,i=0;
System.out.println("Guess a number with a boundary between 1 to 100:");
Scanner input =new Scanner(System.in);
for(i=1;i<=5;i++)
{
System.out.println("Input Number:");
num=input.nextInt();
if(ans==num) break;
if(ans<num)
{
System.out.println("Too big!");
System.out.println("You have only "+(5-i)+" chances!");
continue;
}
if(ans>num)
{
System.out.println("So small!");
System.out.println("You have only "+(5-i)+" chances!");
}
}
if(i<=5) System.out.println("Congratulations!");
else System.out.println("You need more practise and never give up!");
}
}
最简单的实现,未加判断什么的。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
public class fileOperator {
private static String filePath="txt.txt";
private static File file=new File(filePath);
private static int runCount=0;
private static String date=String.format("%tF %<tT", new Date());
private static String os=System.getProperty("os.name");
private static String dataStr="";
public static void main(String args[])
{
loadConfig();
if(dataStr.isEmpty()) System.out.println("First run!");
else
{
System.out.println("runCount: "+runCount+" date:"+date+" os: "+os);
}
putConfig() ;
System.out.println("Hello,java!");
}
private static void loadConfig()
{
try
{
if(!file.exists()) file.createNewFile();
byte[] data=new byte[64];
FileInputStream fis=new FileInputStream(file);
int rs=0;
while((rs=fis.read(data))>0) dataStr+=new String(data,0,rs);
if(!dataStr.isEmpty())
{
String[] sets=dataStr.split(",");
runCount=Integer.parseInt(sets[0]);
date=sets[1];
os=sets[2];
}
fis.close();
}catch(Exception e)
{e.printStackTrace();}
}
private static void putConfig()
{
//String dataStr;
try
{
if(!file.exists()) file.createNewFile();
dataStr=(runCount+1)+","+date+","+os;
byte[] data=dataStr.getBytes();
FileOutputStream fout=new FileOutputStream(file);
fout.write(data);
fout.flush();
fout.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
一个简单的文件操作,将系统信息写入到一个文本文件。
2.猜数字游戏
import java.util.Scanner;
public class guessNumber {
public static void main(String[]args)
{
int ans=(int)(Math.random()*10);
int num,i=0;
System.out.println("Guess a number with a boundary between 1 to 100:");
Scanner input =new Scanner(System.in);
for(i=1;i<=5;i++)
{
System.out.println("Input Number:");
num=input.nextInt();
if(ans==num) break;
if(ans<num)
{
System.out.println("Too big!");
System.out.println("You have only "+(5-i)+" chances!");
continue;
}
if(ans>num)
{
System.out.println("So small!");
System.out.println("You have only "+(5-i)+" chances!");
}
}
if(i<=5) System.out.println("Congratulations!");
else System.out.println("You need more practise and never give up!");
}
}
最简单的实现,未加判断什么的。
相关文章推荐
- C# 用Linq的方式实现对Xml文件的基本操作(创建xml文件、增删改查xml文件节点信息)
- 用Java实现顺序表及其基本操作
- mongoDB——java操作mongoDB实现文件上传下载
- Mybatis原理解析(一)--java.sql数据库操作的基本实现方式
- java加载properties文件的六中基本方式实现
- 使用java实现dos窗口文件操作命令(复制,删除,列出文件目录)
- java实现二叉树基本操作
- java 字符流实现文件读写操作(FileReader-FileWriter)
- Java实现CSV格式文件的读写(操作API)
- 用java简单的实现单链表的基本操作
- java关于文件操作的基本知识Demo
- java文件操作工具类实现复制文件和文件合并
- jldap实现Java对LDAP的基本操作
- C# 用Linq的方式实现对Xml文件的基本操作(创建xml文件、增删改查xml文件节点信息)zz
- java操作hadoop hdfs,实现文件上传下载demo
- Zookeeper客户端基本操作java实现——创建连接、创建节点、添加修改节点内容、获取子节点、获取节点数据、删除节点
- JAVA基础 day21 字节流的输入和输出 字节流的缓冲区 (自己实现BufferedInputStream) 键盘输入 转换流的用法 改变标准输入输出设备 IO流操作的基本规律
- 优先队列的基本操作实现与应用(Java实现)
- java自己实现单链表的基本操作