java高手请进:求一编程

java高手请进:求一编程:编写一个程序,在c盘根目录下创建一个名为aaa.text文件,通过程序将自己的个人信(班级、姓名、学号、年龄、籍贯等)写入该文件中,并将该文件... java高手请进:求一编程:编写一个程序,在c盘根目录下创建一个名为aaa.text文件,通过程序将自己的个人信(班级、姓名、学号、年龄、籍贯等)写入该文件中,并将该文件的内容读写并显示。
功能要求:1、创建一个文件 2、个人信息写入文件 3、读出并显示
展开
 我来答
lbhwrxq
2011-01-03 · TA获得超过764个赞
知道答主
回答量:490
采纳率:0%
帮助的人:269万
展开全部
import java.awt.event.*;
import java.awt.*;
import java.applet.*;

import java.util.Vector;

public class DrawTest extends Applet{
DrawPanel panel;
DrawControls controls;

public void init() {
setLayout(new BorderLayout());
panel = new DrawPanel();
controls = new DrawControls(panel);
add("Center", panel);
add("South",controls);
}

public void destroy() {
remove(panel);
remove(controls);
}

public static void main(String args[]) {
Frame f = new Frame("DrawTest");
DrawTest drawTest = new DrawTest();
drawTest.init();
drawTest.start();

f.add("Center", drawTest);
f.setSize(300, 300);
f.show();
}
public String getAppletInfo() {
return "A simple drawing program.";
}
}

class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
public static final int LINES = 0;
public static final int POINTS = 1;
int mode = LINES;
Vector lines = new Vector();
Vector colors = new Vector();
int x1,y1;
int x2,y2;

public DrawPanel() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}

public void setDrawMode(int mode) {
switch (mode) {
case LINES:
case POINTS:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}

public void mouseDragged(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
x2 = e.getX();
y2 = e.getY();
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x1 = e.getX();
y1 = e.getY();
break;
}
repaint();
}

public void mouseMoved(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
x1 = e.getX();
y1 = e.getY();
x2 = -1;
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
x1 = e.getX();
y1 = e.getY();
repaint();
break;
}
}

public void mouseReleased(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x2 = -1;
break;
case POINTS:
default:
break;
}
repaint();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void paint(Graphics g) {
int np = lines.size();

/* draw the current lines */
g.setColor(getForeground());
for (int i=0; i < np; i++) {
Rectangle p = (Rectangle)lines.elementAt(i);
g.setColor((Color)colors.elementAt(i));
if (p.width != -1) {
g.drawLine(p.x, p.y, p.width, p.height);
} else {
g.drawLine(p.x, p.y, p.x, p.y);
}
}
if (mode == LINES) {
g.setColor(getForeground());
if (x2 != -1) {
g.drawLine(x1, y1, x2, y2);
}
}
}
}

class DrawControls extends Panel implements ItemListener {
DrawPanel target;

public DrawControls(DrawPanel target) {
this.target = target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
CheckboxGroup group = new CheckboxGroup();
Checkbox b;
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.red);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.green);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.blue);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.pink);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.orange);
add(b = new Checkbox(null, group, true));
b.addItemListener(this);
b.setForeground(Color.black);
target.setForeground(b.getForeground());
Choice shapes = new Choice();
shapes.addItemListener(this);
shapes.addItem("Lines");
shapes.addItem("Points");
shapes.setBackground(Color.lightGray);
add(shapes);
}

public void paint(Graphics g) {
Rectangle r = getBounds();
g.setColor(Color.lightGray);
g.draw3DRect(0, 0, r.width, r.height, false);

int n = getComponentCount();
for(int i=0; i<n; i++) {
Component comp = getComponent(i);
if (comp instanceof Checkbox) {
Point loc = comp.getLocation();
Dimension d = comp.getSize();
g.setColor(comp.getForeground());
g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
}
}
}

public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component)e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
}
这是一个applet,把它放在html里面就可以了!
<html>
<applet code=DrawTest.class width=400 height=400>
</applet>
</html>
孤城幻象
2010-12-30
知道答主
回答量:33
采纳率:0%
帮助的人:10.7万
展开全部
import java.io.*;
public class IOTest {

String classname=null;
String name=null;
String stuNo=null;
String stuAge=null;
String stuNative=null;
File f=new File("c:\\aaa.txt");
FileWriter fw=null;
BufferedWriter bw=null;
FileReader fr=null;
BufferedReader br=null;
public IOTest() throws IOException{
boolean b=true;
String s="yes";
String ss="exit";
BufferedReader choice=new BufferedReader(new InputStreamReader(System.in));
while(b){
this.getInfo();
System.out.println("是否继续?"+" "+"继续:yes"+" "+"退出:exit");
String result=choice.readLine();
while(true){
if(result.equals(s)){
b=true;
break;
}else if(result.equals(ss)){
b=false;
break;
}else {
result=choice.readLine();
}
}
}
choice.close();
}
public void getInfo() throws IOException{

System.out.println("loading...");
BufferedReader get=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入班级:");
this.classname=get.readLine();
System.out.println("请输入姓名:");
this.name=get.readLine();
System.out.println("请输入学号:");
this.stuNo=get.readLine();
System.out.println("请输入年龄:");
this.stuAge=get.readLine();
System.out.println("请输入籍贯:");
this.stuNative=get.readLine();
System.out.println("输入完成请输入yes"+" "+"最后回车确认");
String yes=get.readLine();
String s="yes";
while(true){
if(yes.equals(s)){
System.out.println("正在写入...");
this.writeIn();
break;
}else {
yes=get.readLine();
}
}
System.out.println("查看输入结果请输入show"+" "+"回车确认");
String show=get.readLine();
String ss="show";
while(true){
if(show.equals(ss)){
System.out.println("正在读取...");
this.show();
break;
}else {
show=get.readLine();
}
}
}
public void writeIn(){
try {
if(!f.exists()){
f.createNewFile();
}
fw=new FileWriter(f,true);
bw=new BufferedWriter(fw);

String elements="\r\n"+"班级\t"+"姓名\t"+"学号\t"+"年龄\t"+"籍贯"+"\r\n";
bw.write(elements);
bw.write(classname+"\t");
bw.write(name+"\t");
bw.write(stuNo+"\t");
bw.write(stuAge+"\t");
bw.write(stuNative);
System.out.println("写入成功!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void show(){
if(f.exists()){
try {

fr=new FileReader(f);
br=new BufferedReader(fr);
String s="";
while((s=br.readLine())!=null){
System.out.println(s);
}
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else
System.out.println("暂无信息!");

}

public static void main(String args[]) throws IOException{
IOTest test=new IOTest();
}

}

参考资料: 教材and百度

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wodese7en
2010-12-29
知道答主
回答量:22
采纳率:0%
帮助的人:17万
展开全部
public void tests(){
try {
final File file = new File("c:/aaa.txt");
// 判断该路径下是否有aaa.txt的文件
if (!file.exists()) {
file.createNewFile();
}
// 针对文件对象创建写入流对象
final FileWriter fw = new FileWriter(file);
// 为文件写入流创建缓冲流
final BufferedWriter bw = new BufferedWriter(fw);
// 将用户从控制台输入对象转化为字符流,并建立缓冲对象
final BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String str = null;// 用户输入的内容
while (true) {
System.out.println("请输入您的姓名:");
// 接受用户从控制台输入的文字
str += br.readLine() + "\n";
System.out.println("请问您是否还要输入:(y/n)");
if (!br.readLine().equalsIgnoreCase("y")) {
break;
}
System.out.println("请输入您的年龄:");
str += br.readLine() + "\n";
System.out.println("请问您是否还要输入:(y/n)");
if (!br.readLine().equalsIgnoreCase("y")) {
break;
}
bw.write(str);
}
bw.close();
fw.close();
br.close();
} catch (final Exception e) {
e.printStackTrace();
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式