常用经典java代码集锦 java经典编程

java线程的经典代码

package threadgroup;

成都创新互联公司是一家专业提供托克托企业网站建设,专注与网站建设、成都网站制作H5高端网站建设、小程序制作等业务。10年已为托克托众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

class ThreadDemo3 extends Thread {

private String name;

private int delay;

public ThreadDemo3(String sname, int i_delay) {

name = sname;

delay = i_delay;

}

public void run() {

try {

sleep(delay);

} catch (InterruptedException e) {

}

System.out.println("多线程测试!\n" + name + "\n" + delay);

}

}

public class testMyThread {

public static void main(String[] args) {

ThreadDemo3 th1,th2,th3;

th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));

th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900));

th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));

th1.start();

th2.start();

th3.start();

}

}

package threadgroup;

public class threadDemo {

public static void main(String[] args) {

Thread t = Thread.currentThread();

t.setName("你好吗?");

System.out.println("正在进行的Thread是:" + t);

try {

for (int i = 0; i 5; i++) {

System.out.println("我不叫穆继超" + i);

Thread.sleep(3000);

}

} catch (Exception e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

}

}

package threadgroup;

public class threadDemo2 implements Runnable {

public threadDemo2() {

Thread t1 = Thread.currentThread();

t1.setName("第一个主进程");

System.out.println("正在运行" + t1);

Thread t2 = new Thread(this, "");

System.out.println("在创建一个进程");

t2.start();

try {

System.out.println("使他进入第一个睡眠状态");

Thread.sleep(2000);

} catch (InterruptedException e) {

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第一个进程");

}

public void run() {

try {

for (int i = 0; i 5; i++) {

System.out.println("进程" + i);

Thread.sleep(3000);

}

} catch (InterruptedException e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第二个进程");

}

public static void main(String[] args) {

new threadDemo2();

}

}

求助各位前辈关于JAVA的代码~~~~~~~~~~~~~~~~~

至少你现在这个代码“{”比“}”多一个。还有你好像引用了一些不存在的类阿!

class ExitWindow extends WindowAdaper

应该是class ExitWindow extends WindowAdapter

改了一些你的代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MiniDrawPad extends JFrame {

private JButton choices[];

private String names[]={

"New",

"Open",

"Save",

"Pencil",

"Line",

"Rect",

"fRect",

"Oval",

"fOval",

"Circle",

"fCircle",

"RoundRect",

"frRect",

"Rubber",

"Color",

"Stroke",

"Word" };

private Icon items[];

JToolBar buttonPanel ; //定义按钮面板

private JLabel statusBar; //显示鼠标状态的提示条

private DrawPanel drawingArea; //画图区域

private int width=800,height=550; //定义画图区域初始大小

JCheckBox bold,italic; //定义字体风格选择框

JComboBox styles;

public MiniDrawPad() //构造函数

{

super("Drawing Pad");

JMenuBar bar=new JMenuBar(); //定义菜单条

JMenu fileMenu=new JMenu("File");

fileMenu.setMnemonic('F');

//新建文件菜单条

JMenuItem newItem=new JMenuItem("New");

fileMenu.add(newItem);

//保存文件菜单项

JMenuItem saveItem=new JMenuItem("Save");

fileMenu.add(saveItem);

//打开文件菜单项

JMenuItem loadItem=new JMenuItem("Load");

fileMenu.add(loadItem);

fileMenu.addSeparator();

//退出菜单项

JMenuItem exitItem=new JMenuItem("Exit");

fileMenu.add(exitItem);

bar.add(fileMenu);

//设置颜色菜单条

JMenu colorMenu=new JMenu("Color");

//选择颜色菜单项

JMenuItem colorItem=new JMenuItem("Choose Color");

colorMenu.add(colorItem);

bar.add(colorMenu);

JMenu strokeMenu=new JMenu("Stroke");

JMenuItem strokeItem=new JMenuItem("Set Stroke");

strokeMenu.add(strokeItem);

bar.add(strokeMenu);

items=new ImageIcon[names.length];

//创建各种基本图形的按钮

drawingArea=new DrawPanel();

choices=new JButton[names.length];

buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;

buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;

//ButtonHandler handler=new ButtonHandler();

//ButtonHandler1 handler1=new ButtonHandler1();

//导入需要的图形图标,这些图标都存放在与源文件相同的目录下面

for(int i=0;ichoices.length;i++)

{items[i]=new ImageIcon(names[i] + ".gif");

choices[i]=new JButton("",items[i]);

String[] tipText = new String[choices.length];

choices[i].setToolTipText(tipText[i]);

buttonPanel.add(choices[i]);

}

JFrame newPad=new MiniDrawPad();

ExitWindow exit=new ExitWindow();

JFrame frame = new JFrame();

frame.addWindowListener(exit);

}

public static void main(String[] arguments){

}

}

class ExitWindow extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

你那个DrawPanel的类不知道是不是自己定义的,系统里面是没有。现在剩的两个错误就出在这个类上了。还有那个tipText[]也没有定义我也不知道你想写什么就定义了一个空的。frame也没有定义。还有那几个监听都不对,至少不应该定义在main()里面。望能注意一下编码规范,看起来很费事的。

求一个50行左右的JAVA代码,最好每行带注释,谢谢啦

/*这个相当详细了.

程序也不算太难.而且给老师看的时候效果比较好.因为有图形化界面,又实现一个比较实用的功能.老师会比较高兴的.

建立一个文件名为Change.java就可以编译了*/

/*

* 这个程序实现输入身高算出标准体重,输入体重,算出身高的功能

*/

import java.awt.*; //导入相关类包,这才样使用相应awt图形界面的类

import java.awt.event.*;//同上

public class Change extends Frame { //定义一个类Change, 父类是Frame(图形界面的)

Button b = new Button("互查"); //创建一个按钮的对象b,显示为"互查"

Label l1 = new Label("身高(cm)");//创建一个lable.显示身高

Label l2 = new Label("体重(kg)");//创建一个lable 显示体重

double heigth, weigth; //定义变量

double x, y; //定义变量

TextField tf1 = new TextField(null, 10);//添加Text框

TextField tf2 = new TextField(null, 10);//添加Text框

public Change() {//类的构造函数,完成初始化

super("互查表");//创建窗口,标题为互查表

setLayout(new FlowLayout(FlowLayout.LEFT));//设置布局

add(l1);//把lable 身高放到window里

add(tf1);//把Text 框 放到窗口上

add(l2); //把lable 体重放到window里

add(tf2);//Test放到窗口里

add(b);//把button放到窗口上

pack();//自动放到窗口里排列上边的组件

setVisible(true);//可以让用户看到窗口

addWindowListener(new WindowAdapter() {//如果按 X, 关闭窗口

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

b.addActionListener(new ButtonListener());//添加button监听函数

}

class ButtonListener implements ActionListener {//实现click button时功能操作

public void actionPerformed(ActionEvent e) {//当click调用

if (tf1.getText()!=null) {//检查tf1 test 是否为空

try {//取异常

x = Double.parseDouble(tf1.getText());//字符转为double型

weigth = (x - 100) * 0.9;//算重量

tf2.setText("" + weigth);//显示重量

} catch (NumberFormatException ex) {

tf1.setText("");//如果输入不是数字,设为空

}

}

if (tf1.getText().equals("")==true){//tf1是否为空

y = Double.parseDouble(tf2.getText());//把tf2里的文本转为double 型 的

heigth = y / 0.9 + 100; //算身高根据重量

tf1.setText("" + heigth);}//显示身高

}

}

public static void main(String[] args) {//主函数,程序入口

new Change(); //建立类Change的对象,并调用他的构造函数Change().显示窗口

}

}

有什么io方面的java经典代码

package IO;

import java.io.*;

public class FileDirectoryDemo {

public static void main(String[] args) {

// 如果没有指定参数,则缺省为当前目录。

if (args.length == 0) {

args = new String[] { "." };

}

try {

// 新建指定目录的File对象。

File currentPath = new File(args[0]);

// 在指定目录新建temp目录的File对象。

File tempPath = new File(currentPath, "temp");

// 用“tempPath”对象在指定目录下创建temp目录。

tempPath.mkdir();

// 在temp目录下创建两个文件。

File temp1 = new File(tempPath, "temp1.txt");

temp1.createNewFile();

File temp2 = new File(tempPath, "temp2.txt");

temp2.createNewFile();

// 递归显示指定目录的内容。

System.out.println("显示指定目录的内容");

listSubDir(currentPath);

// 更改文件名“temp1.txt”为“temp.txt”。

File temp1new = new File(tempPath, "temp.txt");

temp1.renameTo(temp1new);

// 递归显示temp子目录的内容。

System.out.println("更改文件名后,显示temp子目录的内容");

listSubDir(tempPath);

// 删除文件“temp2.txt”。

temp2.delete();

// 递归显示temp子目录的内容。

System.out.println("删除文件后,显示temp子目录的内容");

listSubDir(tempPath);

} catch (IOException e) {

System.err.println("IOException");

}

}

// 递归显示指定目录的内容。

static void listSubDir(File currentPath) {

// 取得指定目录的内容列表。

String[] fileNames = currentPath.list();

try {

for (int i = 0; i fileNames.length; i++) {

File f = new File(currentPath.getPath(), fileNames[i]);

// 如果是目录,则显示目录名后,递归调用,显示子目录的内容。

if (f.isDirectory()) {

// 以规范的路径格式显示目录。

System.out.println(f.getCanonicalPath());

// 递归调用,显示子目录。

listSubDir(f);

}

// 如果是文件,则显示文件名,不包含路径信息。

else {

System.out.println(f.getName());

}

}

} catch (IOException e) {

System.err.println("IOException");

}

}

}

package IO;

import java.io.*;

public class FileExample {

public FileExample() {

super();

}

public static void main(String[] args) {

try {

String outfile = "demoout.xml";

String infile = "demoin.xml";

/**

* 用FileOutputStream定义一个输入流文件,然后用BuferedOutputStream调用FileOutputStream对象生成一个缓冲输出流

然后用DataOutputStream调用BuferedOutputStream对象生成数据格式化输出流

*/

DataOutputStream dt=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outfile)));

BufferedWriter NewFile = new BufferedWriter(new OutputStreamWriter(dt, "GBK"));

// 对中文的处理

// 定义一个输入流

DataInputStream rafFile1 = new DataInputStream(new BufferedInputStream(new FileInputStream(infile)));

// 定义一个输入缓冲

BufferedReader rafFile = new BufferedReader(new InputStreamReader(rafFile1, "GBK"));

String xmlcontent = "";

char tag = 0;// 文件友字符0结束

while (tag != (char) (-1)) {

xmlcontent = xmlcontent + tag + rafFile.readLine() + '\n';

tag = (char) rafFile.read();

}

NewFile.write(xmlcontent);

NewFile.flush();

NewFile.close();

rafFile.close();

System.gc();

} catch (NullPointerException exc) {

exc.printStackTrace();

} catch (java.lang.IndexOutOfBoundsException outb) {

System.out.println(outb.getMessage());

outb.printStackTrace();

} catch (FileNotFoundException fex) {

System.out.println("fex" + fex.getMessage());

} catch (IOException iex) {

System.out.println("iex" + iex.getMessage());

}

}

}

package IO;

import java.io.*;

public class FileRandomRW {

// 需要输入的person数目。

public static int NUMBER = 3;

public static void main(String[] args) {

Persons[] people = new Persons[NUMBER];

people[0] = new Persons("张峰", 26, 2000, "N");

people[1] = new Persons("艳娜", 25, 50000, "Y");

people[2] = new Persons("李朋", 50, 7000, "F");

try {

DataOutputStream out = new DataOutputStream(new FileOutputStream(

"peoplerandom.dat"));

// 将人员数据保存至“peoplerandom.dat”二进制文件中。

writeData(people, out);

// 关闭流。

out.close();

// 从二进制文件“peoplerandom.dat”中逆序读取数据。

RandomAccessFile inOut = new RandomAccessFile("peoplerandom.dat",

"rw");

Persons[] inPeople = readDataReverse(inOut);

// 输出读入的数据。

System.out.println("原始数据:");

for (int i = 0; i inPeople.length; i++) {

System.out.println(inPeople[i]);

}

// 修改文件的第三条记录。

inPeople[2].setSalary(4500);

// 将修改结果写入文件。

inPeople[2].writeData(inOut, 3);

// 关闭流。

inOut.close();

// 从文件中读入的第三条记录,并输出,以验证修改结果。

RandomAccessFile in = new RandomAccessFile("peoplerandom.dat", "r");

Persons in3People = new Persons();

// 随机读第三条记录。

in3People.readData(in, 3);

// 关闭流。

in.close();

System.out.println("修改后的记录");

System.out.println(in3People);

} catch (IOException exception) {

System.err.println("IOException");

}

}

// 将数据写入输出流。

static void writeData(Persons[] p, DataOutputStream out) throws IOException {

for (int i = 0; i p.length; i++) {

p[i].writeData(out);

}

}

// 将数据从输入流中逆序读出。

static Persons[] readDataReverse(RandomAccessFile in) throws IOException {

// 获得记录数目。

int record_num = (int) (in.length() / Persons.RECORD_LENGTH);

Persons[] p = new Persons[record_num];

// 逆序读取。

for (int i = record_num - 1; i = 0; i--) {

p[i] = new Persons();

// 文件定位。

in.seek(i * Persons.RECORD_LENGTH);

p[i].readData(in, i + 1);

}

return p;

}

}

class Persons {

private String name;

private int age; // 4个字节

private double salary; // 8个字节

private String married;

public static final int NAME_LENGTH = 20; // 姓名长度

public static final int MARRIED_LENGTH = 2; // 婚否长度

public static final int RECORD_LENGTH = NAME_LENGTH * 2 + 4 + 8

+ MARRIED_LENGTH * 2;

public Persons() {

}

public Persons(String n, int a, double s) {

name = n;

age = a;

salary = s;

married = "F";

}

public Persons(String n, int a, double s, String m) {

name = n;

age = a;

salary = s;

married = m;

}

public String getName() {

return name;

}

public int getAge() {

return age;

}

public double getSalary() {

return salary;

}

public String getMarried() {

return married;

}

public String setName(String n) {

name = n;

return name;

}

public int setAge(int a) {

age = a;

return age;

}

public double setSalary(double s) {

salary = s;

return salary;

}

public String setMarried(String m) {

married = m;

return married;

}

// 设置输出格式。

public String toString() {

return getClass().getName() + "[name=" + name + ",age=" + age

+ ",salary=" + salary + ",married=" + married + "]";

}

// 写入一条固定长度的记录,即一个人的数据到输出流。

public void writeData(DataOutput out) throws IOException {

FixStringIO.writeFixString(name, NAME_LENGTH, out);

out.writeInt(age);

out.writeDouble(salary);

FixStringIO.writeFixString(married, MARRIED_LENGTH, out);

}

// 写入一条固定长度的记录到随机读取文件中。

private void writeData(RandomAccessFile out) throws IOException {

FixStringIO.writeFixString(name, NAME_LENGTH, out);

out.writeInt(age);

out.writeDouble(salary);

FixStringIO.writeFixString(married, MARRIED_LENGTH, out);

}

// 随机写入一条固定长度的记录到输出流的指定位置。

public void writeData(RandomAccessFile out, int n) throws IOException {

out.seek((n - 1) * RECORD_LENGTH);

writeData(out);

}

// 从输入流随机读入一条记录,即一个人的数据。

private void readData(RandomAccessFile in) throws IOException {

name = FixStringIO.readFixString(NAME_LENGTH, in);

age = in.readInt();

salary = in.readDouble();

married = FixStringIO.readFixString(MARRIED_LENGTH, in);

}

// 从输入流随机读入指定位置的记录。

public void readData(RandomAccessFile in, int n) throws IOException {

in.seek((n - 1) * RECORD_LENGTH);

readData(in);

}

}

// 对固定长度字符串从文件读出、写入文件

class FixStringIO {

// 读取固定长度的Unicode字符串。

public static String readFixString(int size, DataInput in)

throws IOException {

StringBuffer b = new StringBuffer(size);

int i = 0;

boolean more = true;

while (more i size) {

char ch = in.readChar();

i++;

if (ch == 0) {

more = false;

} else {

b.append(ch);

}

}

// 跳过剩余的字节。

in.skipBytes(2 * (size - i));

return b.toString();

}

// 写入固定长度的Unicode字符串。

public static void writeFixString(String s, int size, DataOutput out)

throws IOException {

int i;

for (i = 0; i size; i++) {

char ch = 0;

if (i s.length()) {

ch = s.charAt(i);

}

out.writeChar(ch);

}

}

}

package IO;

import java.io.*;

import java.util.*;

public class FileRW {

// 需要输入的person数目。

public static int NUMBER = 3;

public static void main(String[] args) {

Person[] people = new Person[NUMBER];

// 暂时容纳输入数据的临时字符串数组。

String[] field = new String[4];

// 初始化field数组。

for (int i = 0; i 4; i++) {

field[i] = "";

}

// IO操作必须捕获IO异常。

try {

// 用于对field数组进行增加控制。

int fieldcount = 0;

// 先使用System.in构造InputStreamReader,再构造BufferedReader。

BufferedReader stdin = new BufferedReader(new InputStreamReader(

System.in));

for (int i = 0; i NUMBER; i++) {

fieldcount = 0;

System.out.println("The number " + (i + 1) + " person");

System.out

.println("Enter name,age,salary,married(optional),please separate fields by ':'");

// 读取一行。

String personstr = stdin.readLine();

// 设置分隔符。

StringTokenizer st = new StringTokenizer(personstr, ":");

// 判断是否还有分隔符可用。

while (st.hasMoreTokens()) {

field[fieldcount] = st.nextToken();

fieldcount++;

}

// 如果输入married,则field[3]不为空,调用具有四个参数的Person构造函数。

if (field[3] != "") {

people[i] = new Person(field[0],

Integer.parseInt(field[1]), Double

.parseDouble(field[2]), field[3]);

// 置field[3]为空,以备下次输入使用。

field[3] = "";

}

// 如果未输入married,则field[3]为空,调用具有三个参数的Person构造函数。

else {

people[i] = new Person(field[0],

Integer.parseInt(field[1]), Double

.parseDouble(field[2]));

}

}

// 将输入的数据保存至“people.dat”文本文件中。

PrintWriter out = new PrintWriter(new BufferedWriter(

new FileWriter("people.dat")));

writeData(people, out);

// 关闭流。

out.close();

// 从文件“people.dat”读取数据。

BufferedReader in = new BufferedReader(new FileReader("people.dat"));

Person[] inPeople = readData(in);

// 关闭流。

in.close();

// 输出从文件中读入的数据。

for (int i = 0; i inPeople.length; i++) {

System.out.println(inPeople[i]);

}

} catch (IOException exception) {

System.err.println("IOException");

}

}

// 将所有数据写入输出流。

static void writeData(Person[] p, PrintWriter out) throws IOException {

// 写入记录条数,即人数。

out.println(p.length);

for (int i = 0; i p.length; i++) {

p[i].writeData(out);

}

}

// 将所有数据从输入流中读出。

static Person[] readData(BufferedReader in) throws IOException {

// 获取记录条数,即人数。

int n = Integer.parseInt(in.readLine());

Person[] p = new Person[n];

for (int i = 0; i n; i++) {

p[i] = new Person();

p[i].readData(in);

}

return p;

}

}

class Person {

private String name;

private int age;

private double salary;

private String married;

public Person() {

}

public Person(String n, int a, double s) {

name = n;

age = a;

salary = s;

married = "F";

}

public Person(String n, int a, double s, String m) {

name = n;

age = a;

salary = s;

married = m;

}

public String getName() {

return name;

}

public int getAge() {

return age;

}

public double getSalary() {

return salary;

}

public String getMarried() {

return married;

}

// 设置输出格式。

public String toString() {

return getClass().getName() + "[name=" + name + ",age=" + age

+ ",salary=" + salary + ",married=" + married + "]";

}

// 写入一条记录,即一个人的数据到输出流。

public void writeData(PrintWriter out) throws IOException {

// 格式化输出。

out.println(name + ":" + age + ":" + salary + ":" + married);

}

// 从输入流读入一条记录,即一个人的数据。

public void readData(BufferedReader in) throws IOException {

String s = in.readLine();

StringTokenizer t = new StringTokenizer(s, ":");

name = t.nextToken();

age = Integer.parseInt(t.nextToken());

salary = Double.parseDouble(t.nextToken());

married = t.nextToken();

}

}

package IO;

import java.io.*;

public class IOStreamExample {

public static void main(String[] args) throws IOException {

// 1. 读入一行数据:

BufferedReader in = new BufferedReader(new FileReader(

"FileStdRead.java"));

String s, s2 = new String();

while ((s = in.readLine()) != null) {

s2 += s + "\n";

}

in.close();

BufferedReader stdin = new BufferedReader(new InputStreamReader(

System.in));

System.out.print("Enter a line:");

System.out.println(stdin.readLine());

// 2. 从内存中读入

StringReader in2 = new StringReader(s2);

int c;

while ((c = in2.read()) != -1) {

System.out.print((char) c);

}

// 3. 格式化内存输入

try {

DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(

s2.getBytes()));

while (true) {

System.out.print((char) in3.readByte());

}

} catch (EOFException e) {

System.err.println("End of stream");

}

// 4. 文件输入

try {

BufferedReader in4 = new BufferedReader(new StringReader(s2));

PrintWriter out1 = new PrintWriter(new BufferedWriter(

new FileWriter("IODemo.out")));

int lineCount = 1;

while ((s = in4.readLine()) != null) {

out1.println(lineCount++ + ": " + s);

}

out1.close();

} catch (EOFException e) {

System.err.println("End of stream");

}

// 5. 接收和保存数据

try {

DataOutputStream out2 = new DataOutputStream(

new BufferedOutputStream(new FileOutputStream("Data.txt")));

out2.writeDouble(3.14159);

out2.writeUTF("That was pi");

out2.writeDouble(1.41413);

out2.writeUTF("Square root of 2");

out2.close();

DataInputStream in5 = new DataInputStream(new BufferedInputStream(

new FileInputStream("Data.txt")));

System.out.println(in5.readDouble());

System.out.println(in5.readUTF());

System.out.println(in5.readDouble());

System.out.println(in5.readUTF());

} catch (EOFException e) {

throw new RuntimeException(e);

}

// 6. 随机读取文件内容

RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw");

for (int i = 0; i 10; i++) {

rf.writeDouble(i * 1.414);

}

rf.close();

rf = new RandomAccessFile("rtest.dat", "rw");

rf.seek(5 * 8);

rf.writeDouble(47.0001);

rf.close();

rf = new RandomAccessFile("rtest.dat", "r");

for (int i = 0; i 10; i++) {

System.out.println("Value " + i + ": " + rf.readDouble());

}

rf.close();

}

}

package IO;

import java.io.*;

/**

* p

* Title: JAVA进阶诀窍

* /p

*

* @author 张峰

* @version 1.0

*/

public class MakeDirectoriesExample {

private static void fileattrib(File f) {

System.out.println("绝对路径: " + f.getAbsolutePath() + "\n 可读属性: "

+ f.canRead() + "\n 可定属性: " + f.canWrite() + "\n 文件名: "

+ f.getName() + "\n 父目录: " + f.getParent() + "\n 当前路径: "

+ f.getPath() + "\n 文件长度: " + f.length() + "\n 最后更新日期: "

+ f.lastModified());

if (f.isFile()) {

System.out.println("输入的是一个文件");

} else if (f.isDirectory()) {

System.out.println("输入的是一个目录");

}

}

public static void main(String[] args) {

if (args.length 1) {

args = new String[3];

}

args[0] = "d";

args[1] = "test1.txt";

args[2] = "test2.txt";

File old = new File(args[1]), rname = new File(args[2]);

old.renameTo(rname);

fileattrib(old);

fileattrib(rname);

int count = 0;

boolean del = false;

if (args[0].equals("d")) {

count++;

del = true;

}

count--;

while (++count args.length) {

File f = new File(args[count]);

if (f.exists()) {

System.out.println(f + " 文件己经存在");

if (del) {

System.out.println("删除文件" + f);

f.delete();

}

} else { // 如果文件不存在

if (!del) {

f.mkdirs();

System.out.println("创建文件: " + f);

}

}

fileattrib(f);

}

}

}

谁能给一个Java程序代码我,要50行到100行就可以啦。最好有几行解释

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Properties;

/**

* java应用对配置文件的读写

*

* @author

* @version

*

*/

public class PropertyMgr {

private static Properties props = new Properties();

private static FileInputStream in = null;

private static FileOutputStream out = null;

static {

try {

// 从当前类路径下的配置文件

props.load(PropertyMgr.class.getClassLoader().getResourceAsStream("System.properties"));

// 从指定文件名读入配置信息

in = new FileInputStream("System.properties");

props.load(in);

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 获取键所对应的值

*

* @param key

* 要得到值的键

* @return

*/

public static String getProperty(String key) {

return props.getProperty(key);

}

/**

* 改变或添加一个key的值 当key存在于properties文件中时该key的值被value所代替, 当key不存在时,该key的值是value

*

* @param key

* 要存入的键

* @param value

* 要存入的值

*/

public static String setProperty(String key, String value) {

try {

props.setProperty(key, value);

} catch (RuntimeException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

String urlFile = new File("System.properties").getAbsolutePath();

String tempUrl = urlFile.substring(urlFile.indexOf(File.separator));

out = new FileOutputStream(tempUrl);

props.store(out, null);

out.close();

} catch (FileNotFoundException e) {

System.out.println("系统找不到指定的路径");

e.printStackTrace();

} catch (IOException ioe) {

ioe.printStackTrace();

}

return "页面名称保存成功";

}

/**

* 将更改后的文件数据存入指定的文件中,该文件可以不存在,但是路径文件所在的文件夹一定得存在。

*

* @param fileName

* 文件路径+文件名称

* @param description

* 对该文件的描述

*/

public static String saveFile() {

try {

String urlFile = new File("System.properties").getAbsolutePath();

String tempUrl = urlFile.substring(urlFile.indexOf(File.separator));

System.out.println("tempUrl:" + tempUrl);

out = new FileOutputStream(tempUrl);

props.store(out, null);

out.close();

} catch (FileNotFoundException e) {

System.out.println("系统找不到指定的路径");

e.printStackTrace();

} catch (IOException ioe) {

ioe.printStackTrace();

}

return "保存成功";

}

}


网站题目:常用经典java代码集锦 java经典编程
转载源于:http://scyanting.com/article/higdsp.html