一个JAVA问题,急求解决!!!

作者&投稿:禤法 (若有异议请与网页底部的电邮联系)
一个JAVA小问题,急求高人解决!!!~

FileSearch实现了操作系统文件夹中对文件名中含有指定字符串的查找,会在控制台输出所有符合条件的文件路径并且最终输出总数。
FileSearch2实现了操作系统文件夹中所有txt文件的查找,原理相同。FileOperation 接口是辅助接口,用于通用化文件迭代

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class FileSearch {
private long count = 0;

public long getCount() {
return count;
}

public void search(File path, final String name) {
doWithFiles(path, new FileOperation() {
public void operate(File f) {
if (f.getName().indexOf(name) >= 0) {
count++;
System.out.println(f.getAbsolutePath());
}
}
});
}

public static void main(String[] args) throws Exception {
String winpath = getWinPath();
System.out.println(winpath);
FileSearch fileSearch = new FileSearch();
fileSearch.search(new File(winpath), "setup");
System.out.println(fileSearch.getCount());
}

private static String getWinPath() throws IOException,
InterruptedException, UnsupportedEncodingException {
Process p = Runtime.getRuntime().exec("cmd /c echo %windir%");
p.waitFor();
StringBuffer sb = new StringBuffer();
byte[] buffer = new byte[256];
int cnt = 0;
InputStream is = p.getInputStream();
while((cnt = is.read(buffer))>0) {
sb.append(new String(buffer,0,cnt,"ASCII"));
}
String winpath = sb.toString().trim();
return winpath;
}

public void doWithFiles(File f, FileOperation fo) {
if (f.isFile()) {
fo.operate(f);
} else if (f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File ff = files[i];
doWithFiles(ff, fo);
}
}
}
}

}

class FileSearch2 {
private long count = 0;

public long getCount() {
return count;
}

public void search(File path, final String name) {
doWithFiles(path, new FileOperation() {
public void operate(File f) {
if (f.getName().endsWith(name)) {
count++;
System.out.println(f.getAbsolutePath());
}
}
});
}

public static void main(String[] args) throws Exception {
String winpath = getWinPath();
System.out.println(winpath);
FileSearch fileSearch = new FileSearch();
fileSearch.search(new File(winpath), ".txt");
System.out.println(fileSearch.getCount());
}

private static String getWinPath() throws IOException,
InterruptedException, UnsupportedEncodingException {
Process p = Runtime.getRuntime().exec("cmd /c echo %windir%");
p.waitFor();
StringBuffer sb = new StringBuffer();
byte[] buffer = new byte[256];
int cnt = 0;
InputStream is = p.getInputStream();
while((cnt = is.read(buffer))>0) {
sb.append(new String(buffer,0,cnt,"ASCII"));
}
String winpath = sb.toString().trim();
return winpath;
}

public void doWithFiles(File f, FileOperation fo) {
if (f.isFile()) {
fo.operate(f);
} else if (f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File ff = files[i];
doWithFiles(ff, fo);
}
}
}
}

}
interface FileOperation {
public void operate(File f);
}

public class Employee {private String name;private String department;private int salary;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDepartment() {return department;}public void setDepartment(String department) {this.department = department;}public int getSalary() {return salary;}public void setSalary(int salary) {this.salary = salary;}}import java.util.ArrayList;


public class Test {

private static Employee [] eArray = new Employee[3];
private static ArrayList arrayList = new ArrayList();
public static void main(String[] args) {
Employee e1 = new Employee();
e1.setName("E1");
e1.setDepartment("QA1");
e1.setSalary(8000);
Employee e2 = new Employee();
e2.setName("E2");
e2.setDepartment("QA2");
e2.setSalary(9000);
Employee e3 = new Employee();
e3.setName("E3");
e3.setDepartment("QA3");
e3.setSalary(7000);
//array
eArray[0] = e1;
eArray[1] = e2;
eArray[2] = e3;
//ArrayList
arrayList.add(e1);
arrayList.add(e2);
arrayList.add(e3);
System.out.println("Array------------");
for (Employee e : eArray) {
System.out.println("姓名:"+e.getName()+" 部门:"+e.getDepartment()+" 加薪后工资:"+raiseSalary((e.getSalary()),0.07));
}
System.out.println("ArrayList------------");
for (Employee e : arrayList) {
System.out.println("姓名:"+e.getName()+" 部门:"+e.getDepartment()+" 加薪后工资:"+raiseSalary((e.getSalary()),0.07));
}
}

public static double raiseSalary(int s,double r){
return s*(1+r);
}
}

//////////////////////Account//////////////////////////////////
public class Account {
private String name_ = null;
private Double money_ = 0.0d;
private Double saved_ = 0.0d;
private Double taken_ = 0.0d;
public Account(String name, double money) {
this.name_ = name;
this.money_ = money;
}
public synchronized void save(double money) {
this.money_ += money;
this.saved_ += money;
display("save", money, true);
}
public synchronized void take(double money) {
if (this.money_ >= money) {
this.money_ -= money;
this.taken_ += money;
display("take", money, true);
} else {
display("take", money, false);
}
}
public void displayAll() {
StringBuilder sb = new StringBuilder();
sb.append("Account:" + this.name_);
sb.append(" Saved:" + this.saved_);
sb.append(" Taken:" + this.taken_);
sb.append(" Money:" + this.money_);
if (this.money_ + this.taken_ == this.saved_) {
sb.append(" Reslut: true");
} else {
sb.append(" Reslut: false");
}
System.out.println(sb.toString());
}
public void display(String op, double money, boolean result) {
StringBuilder sb = new StringBuilder();
sb.append("Account:" + this.name_);
if (null != op) {
sb.append(" Operation:" + op);
sb.append(" " + money);
sb.append(" " + result);
}
sb.append(" Money:" + this.money_);
sb.append(" Thread:" + Thread.currentThread().getId());
System.out.println(sb.toString());
}
}

//////////////////////SyncDemo//////////////////////////////////
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class SyncDemo implements Runnable {
static boolean cancel = false;
private Account account = new Account("zhangsan", 0);
public static void main(String[] args) {
SyncDemo syn = new SyncDemo();
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 2; i++) {
exec.execute(syn);
}
try {
Thread.sleep(2000);
syn.cancel();
exec.shutdown();
if (!exec.awaitTermination(1000, TimeUnit.MILLISECONDS)) {
System.out.println("Some tasks were not terminated!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
syn.displayAll();
}
private void cancel() {
cancel = true;
}
private void displayAll() {
this.account.displayAll();
}
public void run() {
Random random = new Random();
while (!cancel) {
if (random.nextBoolean()) {
this.account.save(random.nextInt(100));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
this.account.take(random.nextInt(100));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

账户类设定属性名称 密码 账户上的钱
主程序设定方法登陆 取款 查询 注销等
基本思路就是这样 具体写出来也不难 最好还是自己写吧

public class Account
{
privateString 账户ID;
private ...
private double 账户余额;

public void set账户余额(double num)
{
this.账户余额 = num;
}

public double get账户余额()
{
return 账户余额;
}

}

public class SyncDemo
{
private boolean 存钱(double num)
{
Account user1 = new Account();
try{
//锁数据
double 余额 = user1.get账户余额();
user1.set账户余额(余额 + num);
//解锁
return true;
}
catch(Exception e)
{
rollback;
return false;
}
}

private boolean 取钱(double num)
{

}

}


求助JAVA 问题
5. true flase 6.float[] floatAry ={2.3, 7.5} 7.方法 属性 8.abstract 9. jar myjava 10.inner 二、简答题(8*5=40)1. Java的基本数据类型及其字节数。byte 1个字节 short 2个字节 char 2个字节 int 4个字节 long 8个字节 float 4个字节 double 8个字...

java 问题
1.没有区别。JRE是运行环境。比如你做了一个JAVA的程序来给别人使用。他只需要安装JRE就可以。而不需要安装JDK。这个好象在1.5开始是这样的。那时的下载说明我记得是JDK+JRE这样说的说明。2.你缺少servlet-api.jar这个包。看了你给出的链接,那是基本配置。只是能让你Java基本运行起来。servlet-api....

求助,关于java,的问题,急,谢谢
二、读程序,写出程序运行后的结果。 1、class P2_1 这一题是因为 首先定义的是int a=2,b; 所以a,b都是int类型的 然后a\/10*10先算2\/10等于0.2但是int类型是没有小数的所有0.几的小数都默认是等于0再算0*10=0所以结果是0 2、这一题也是简单的main函数的程序的入口所以先执行main...

关于JAVA问题的解答
第1题 :下面哪个对类的声明是错误的? (A),Java中没有多继承,C++有多继承,Java只能多实现多个接口;第2题 某一个子类要继承一个父类,要使用关键字(extends )。第3个题:下列说法正确的有( C)Java中new 的时候执行构造方法,至于执行哪一个看你new的哪一个构造方法 第4题:有以下...

java问题求答案.急!!
1、开发与运行Java程序需要经过的三个主要步骤为 编辑源程序、编译生成字节码 和 解释运行字节码。2、 设x = 2 ,则表达式 ( x + + ) * 3 的值是( 6 )。3、 据程序的构成和运行环境的不同,Java源程序分为两大类:java application程序和 applet程序。4、 一个Java Application源程序文件...

关于JAVA的两个题,希望大神解决一下,马上快交作业了?
关于JAVA的两个题,希望大神解决一下,马上快交作业了? 规则问题之类的截图上有,希望帮忙一下,解决好了有追加。急急急... 规则问题之类的截图上有,希望帮忙一下,解决好了有追加。急急急 展开  我来答 2个回答 #热议# 牙齿是越早矫正越好吗?

我这里有个java的问题不明白希望大神帮忙解决!
你这里又两个问题:这是个算数优先级的问题,会进行先++a,然后进行判断a++后的值是否与b相等,最后再进行b--所以结果当然就不相等了 你的算数相当于3!=2 所以是true

java简单问题,答案都书本都给出来了,可是都没懂,求助高手解释一下哈!先...
6.因为S1和S2对应的不是同一对象,对于两个对象之间使用“==”比较的是他们对应的内存地址是否相同,用equals比较才是比较之间的内容是否相同 13 数组必须这样定义,可以定义a[1][],a[][],a[1][1],但是不可以定义a[][1]因为产生数据的时候,会发现二维数组无法找到固定的一维数组作为支撑。至于...

java题,正在考试,急!
第三题,不知道是不是要这种,不一定对!~1.①所谓抽象类就是只声明方法的存在而不去具体实现它的类。抽象类不能被实例化,也就是不能创建其对象;②不能 2.①如果在子类中定义的一个方法,其名称、返回类型及参数签名正好与父类中某个方法的名称、返回类型及参数签名相匹配,那么可以说,子类的...

java的题,谁会,急用,等待中。
\/\/当到达第N个位置的时候就停止输出 while(i<=n){ System.out.println(function(i));\/\/每输出一个就从后面加一位,即输出下一位的数 i++;} } public static void main(String[] args) { \/\/传入10,输出前10个数 print(10);} } 第二题:import java.util.Scanner;public class Test2 ...

蓬安县19757185835: 一个JAVA问题,请求帮忙解决 -
韦包硝酸: Name=Name.substring(0,1); Name=Name.substring(1); 你的Name变量值被冲掉,正确的是:String lastName=Name.substring(0,1); System.out.println("\n\n姓氏:"+lastName); String firstName=Name.substring(1); System.out.println("名字:"+firstName); 不过正如楼上说的,输入姓,名应该分开,并且中国和外国次序不一样.并且Java 里面变量首字母最好小写.

蓬安县19757185835: Java问题,急急急,求帮忙 -
韦包硝酸: 我是学c和c 的,不过我倒是能看懂你的这几句代码,你这里是用的递归算法吧,下面我们对你getvalue函数进行分析(一下简称get); 你给get的初值是6, 就运行getvalue(n-1) getvalue(n-2)= getvalue(6-1) getvalue(6-2)= getvalue(5) getvalue(4)...

蓬安县19757185835: 一个java程序问题,求答案?
韦包硝酸: 简单的解决办法,点左边第一个X,选择第一项,目的:加上package 看看有没有用到File.count这个类,有的话去找找你复制的地方(这程序明显你复制的) 没有用到的话直接删掉着行

蓬安县19757185835: 一道Java问题,急求解!
韦包硝酸: import java.util.Scanner; public class Test40023{ public static void main(String args[]){ int ri, repeat; int count, digit, i, j, k, m, n, sum; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri<=repeat; ri++){ m=in.nextInt(); n=in.nextInt(); ...

蓬安县19757185835: Java问题,求解,急...
韦包硝酸:public class Test{ public static void main(String[] args){ int sum = 0; for( int i = 1; i <= 99; i += 2){ sum += i; } System.out.println("Sum=" + sum); } }

蓬安县19757185835: 谁能帮我解决这个java的问题?急.... -
韦包硝酸: 错误行数 5改正:for (int j = 0 ; j < 10; j++) {} 数组下标是从0开始的,不是1,这个记住就行了

蓬安县19757185835: java问题求答案.急!! -
韦包硝酸: 1、开发与运行Java程序需要经过的三个主要步骤为 编辑源程序、编译生成字节码 和 解释运行字节码.2、 设x = 2 ,则表达式 ( x + + ) * 3 的值是( 6 ).3、 据程序的构成和运行环境的不同,Java源程序分为两大类:java application程序和 ...

蓬安县19757185835: 请高手帮我解决一个JAVA编程问题
韦包硝酸: 求最大公约数和最小公倍数 import java.util.*; public class ComputeCommon { public int greatestCommonDivisor(int m,int n){ int k=-1; while(k!=0){ k=m%n; m=n; n=k; } return m; } public int leaseCommonMultiple(int m,int n){ int k=m*n/...

蓬安县19757185835: 求解一道java题~ 很急,有追加~ -
韦包硝酸: 这个就是答案,采纳 import java.util.Scanner; public class ReplaceNumber { public static void main ( String[] args ) { System.out.println ("输入一个正整数n,输出n行的三角型:"); Scanner scanner = new Scanner (System.in); while (...

蓬安县19757185835: java的一些问题!急!帮助!十分感谢. -
韦包硝酸: 1.是同一父类2.可以又抽象方法3.不一定,private修饰的变量和方法不可被子类继承,在子类中可以增加子类的变量和方法3.(你写重复了)所有子类在产生对象是,都会默认去调用父...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网