请教一个“Java代码中的错误原因”.
高手,您好:
我是一名学习软件工程的学生,现在,我正在做我的第一个Java作品。
希望得到高手的点拨:
下面我写的这个“查找本地文件”的代码:
package c_port_package;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
public class CheckAndConfirmworker {
static C_port_Our_honor fileDS = null;
HashMap<String,String> fileset = null;
static ArrayList<String> thecommonfruitPath = new ArrayList<String>();
static ArrayList<String> thevideofruitPath = new ArrayList<String>();
static ArrayList<String> thecommonfruitFullname = new ArrayList<String>();
static ArrayList<String> thecommonfruitExtenName = new ArrayList<String>();
static ArrayList<String> thecommonfruitpreName = new ArrayList<String>();
static ArrayList<String> thecommonvideoExtenName = new ArrayList<String>();
static ArrayList<String> thevideoFullName = new ArrayList<String>();
static ArrayList<String> thevideofruitpreName = new ArrayList<String>();
static ArrayList<String> thevideofruitFullName = new ArrayList<String>();
static ArrayList<String> thevideofruitExtenName = new ArrayList<String>();
static int calc0 = 0;
static int calc1 = 0;
static File file = new File("D:\");
static int j = 0;
static int t = 1;
//获取文件扩展名
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
//获取去掉扩展名的文件名
public static String getpreFileName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
public static void main(String[] args){
for(int i = 0;i<2;i++){
System.out.println(thecommonfruitPath.get[i]);
}
}
static public ArrayList<String> findthecommonfruit(File file){
File[] files1 = null;
files1 = file.listFiles();
for(int i=0;i<files1.length;i++){
if(files1[i].isDirectory()){
findthecommonfruit(files1[i]);
}
for(i=0;i<files1.length;i++){
if(files1[i].isDirectory()){
findthevideofruit(files1[i]);
++calc0;
}
//System.out.println(files[i]);
else if(files1[i].getPath().endsWith("ppt")||files1[i].getPath().endsWith("doc")||files1[i].getPath().endsWith("txt")||files1[i].getPath()
.endsWith("wps")||files1[i].getPath().endsWith("excel")||files1[i].getPath().endsWith("jude")||files1[i].getPath().endsWith("bmp")
||files1[i].getPath().endsWith("c")||files1[i].getPath().endsWith("cpp")||files1[i].getPath().endsWith("java")||files1[i].getPath().endsWith("js")
||files1[i].getPath().endsWith("asp")||files1[i].getPath().endsWith("jsp")||files1[i].getPath().endsWith("php")||files1[i].getPath().endsWith("pdf")
||files1[i].getPath().endsWith("jpeg")||files1[i].getPath().endsWith("gif")||files1[i].getPath().endsWith("html")||files1[i].getPath().endsWith("png")
||files1[i].getPath().endsWith("xls")||files1[i].getPath().endsWith("zip")){
thecommonfruitPath.add(files1[i].getAbsolutePath());
thecommonfruitFullname.add(files1[i].getName());
thecommonfruitpreName.add(getpreFileName(files1[i].getName()));
thecommonfruitExtenName.add(getExtensionName(files1[i].getName()));
++calc1;
}
}
}
return thecommonfruitPath;
}
static public ArrayList<String> findthevideofruit(File file){
File[] files2 = null;
files2 = file.listFiles();
for(int i=0;i<files2.length;i++){
if(files2[i].isDirectory()){
findthecommonfruit(files2[i]);
}
for(i=0;i<files2.length;i++){
if(files2[i].isDirectory()){
findthevideofruit(files2[i]);
++calc0;
}
//System.out.println(files[i]);
else if(files2[i].getPath().endsWith("3GP")||files2[i].getPath().endsWith("mid")||files2[i].getPath().endsWith("mp4")||files2[i].getPath()
.endsWith("flash")||files2[i].getPath().endsWith("rm")||files2[i].getPath().endsWith("rmvb")||files2[i].getPath().endsWith("wma")
||files2[i].getPath().endsWith("avi")){
thevideofruitPath.add(files2[i].getAbsolutePath());
thevideofruitFullName.add(files2[i].getName());
thevideofruitpreName.add(getpreFileName(files2[i].getName()));
thevideofruitExtenName.add(getExtensionName(files2[i].getName()));
++calc1;
}
}
}
return thevideofruitPath;
}
public ArrayList<String> giveCsectorcommonFilepath(){
return thecommonfruitPath=findthecommonfruit(new File("C:"+File.separator));
}
public ArrayList<String> giveCsectorvideoFilepath(){
return thevideofruitPath=findthevideofruit(new File("C:"));
}
public ArrayList<String> giveDsectorcommonFilepath(){
return findthecommonfruit(new File("D:"+File.separator));
}
public ArrayList<String> giveDvideocommonFilepath(){
return findthevideofruit(new File("D:"+File.separator));
}
public ArrayList<String> giveEsectorcommonfilepath(){
return findthecommonfruit(new File("E:"+File.separator));
}
public ArrayList<String> giveEsectorvideofilepath(){
return findthevideofruit(new File("E:"+File.separator));
}
public ArrayList<String> giveFsectorcommonfilepath(){
return findthecommonfruit(new File("F:"+File.separator));
}
public static ArrayList<String> giveFsectorvideofilepath(){
return findthevideofruit(new File("F:"+File.separator));
}
}
希望得到诸位高手的点拨:
我上面的这个“第52行代码”中的错误,是由什么原因,造成的...?
怎么修改,能够将错误进行排出,并且,将本类文件的“查找本地文件”的功能,实现...?
谢谢高手的点拨!!
一百分奉上!!
一位日日夜夜向着理想奔跑的筑梦者
2013年8月11日早晨4点02分 Java 调试 Java?SE排查错误 Java代码编译错误原因
[解决办法]
难道你从来没有写过程序吗- -!
main函数是程序入口,意味着你想要做的事情,都需要在main里做,你注意下你现在main里做了什么:仅仅是循环了下一个List而已。。你想要查找本地文件,那你要调用对应的方法才行啊,方法你都写了,但是你没有在main里调用,怎么会有结果呢。
[解决办法]
改为 for(int i = 0;i<2;i++){
System.out.println(findthecommonfruit(file).get[i]);
}
[解决办法]
没调相应的方法
[解决办法]
大家来猜楼主那第52行的错误是啥, 既然楼主这么慷慨的居然没把错误本身贴出来,分明是让大家猜啊。
我猜是该变量无get方法
[解决办法]
thecommonfruitPath=findthecommonfruit(File file)
[解决办法]
主函数没有调用子方法么,
[解决办法]
public static void main(String[] args) {
findthecommonfruit(new File("F:\\music"));//这里需要执行这个调用方法,并给出需要搜索的路径
//循环遍历,需要用size()方法
for (int i = 0; i < thecommonfruitPath.size(); i++) {
System.out.println(thecommonfruitPath.get(i));
}
}
public ArrayList<String> giveCsectorcommonFilepath(){
return thecommonfruitPath=findthecommonfruit(new File("C:"+File.separator));
}
public ArrayList<String> giveCsectorvideoFilepath(){
return thevideofruitPath=findthevideofruit(new File("C:"));
}
public ArrayList<String> giveDsectorcommonFilepath(){
return findthecommonfruit(new File("D:"+File.separator));
}
public ArrayList<String> giveDvideocommonFilepath(){
return findthevideofruit(new File("D:"+File.separator));
}
public ArrayList<String> giveEsectorcommonfilepath(){
return findthecommonfruit(new File("E:"+File.separator));
}
public ArrayList<String> giveEsectorvideofilepath(){
return findthevideofruit(new File("E:"+File.separator));
}
public ArrayList<String> giveFsectorcommonfilepath(){
return findthecommonfruit(new File("F:"+File.separator));
}
thecommonfruitPath = giveFsectorcommonfilepath();
for(int i = 0;i<2;i++){
System.out.println(thecommonfruitPath.get(i));
}
[解决办法]
thecommonfruitPath?对象,是个链表,不是数组。
所以,不能使用下角标的那个符号,应该使用函数调用的方法。
也就是说,不能用方括号,应该用圆括号。以为你是调用的get方法(函数)。
System.out.println(thecommonfruitPath.get(i));
[解决办法]
package com.sisj.Test;
import java.io.File;
import java.util.ArrayList;
public class CheckAndConfirmworker {
private static ArrayList<String> commonPath = new ArrayList<String>();
private static ArrayList<String> videoPath = new ArrayList<String>();
private static ArrayList<String> commonFullname = new ArrayList<String>();
private static ArrayList<String> commonExtendName = new ArrayList<String>();
private static ArrayList<String> commonPreName = new ArrayList<String>();
private static ArrayList<String> videoFullName = new ArrayList<String>();
private static ArrayList<String> videoPreName = new ArrayList<String>();
private static ArrayList<String> videoExtenName = new ArrayList<String>();
public static void main(String[] args) {
try {
// 获取文件路径
File file = new File("D:\\Test");
commonPath = findCommon(file);
} catch (Exception e) {
e.printStackTrace();
//Do something.
}
for (int i = 0; i < commonPath.size(); i++) {
System.out.println(commonPath.get(i));
}
}
/**
* 普通文件取得
*
* @param file
* @return ArrayList
*/
public static ArrayList<String> findCommon(File file) {
File[] normalFiles = null;
normalFiles = file.listFiles();
for (int i = 0; i < normalFiles.length; i++) {
for (i = 0; i < normalFiles.length; i++) {
if (normalFiles[i].isDirectory()) {
findCommon(normalFiles[i]);
findVideo(normalFiles[i]);
} else if (normalFiles[i].getPath().toLowerCase().endsWith("ppt")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("doc")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("txt")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("wps")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("excel")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("jude")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("bmp")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("c")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("cpp")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("java")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("js")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("asp")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("jsp")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("php")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("pdf")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("jpeg")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("gif")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("html")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("png")
------解决方案--------------------
normalFiles[i].getPath().toLowerCase().endsWith("xls")
[解决办法]
normalFiles[i].getPath().toLowerCase().endsWith("zip")) {
commonPath.add(normalFiles[i].getAbsolutePath());
commonFullname.add(normalFiles[i].getName());
commonPreName.add(getPreFileName(normalFiles[i].getName()));
commonExtendName.add(getExtensionName(normalFiles[i].getName()));
}
}
}
return commonPath;
}
/**
* 音视频文件取得
*
* @param file
* @return ArrayList
*/
public static ArrayList<String> findVideo(File file) {
File[] videoFiles = null;
videoFiles = file.listFiles();
for (int i = 0; i < videoFiles.length; i++) {
for (i = 0; i < videoFiles.length; i++) {
if (videoFiles[i].isDirectory()) {
findCommon(videoFiles[i]);
findVideo(videoFiles[i]);
} else if (videoFiles[i].getPath().toLowerCase().endsWith("3gp")
[解决办法]
videoFiles[i].getPath().toLowerCase().endsWith("mid")
[解决办法]
videoFiles[i].getPath().toLowerCase().endsWith("mp4")
[解决办法]
videoFiles[i].getPath().toLowerCase().endsWith("flash")
[解决办法]
videoFiles[i].getPath().toLowerCase().endsWith("rm")
[解决办法]
videoFiles[i].getPath().toLowerCase().endsWith("rmvb")
[解决办法]
videoFiles[i].getPath().toLowerCase().endsWith("wma")
[解决办法]
videoFiles[i].getPath().toLowerCase().endsWith("avi")) {
videoPath.add(videoFiles[i].getAbsolutePath());
videoFullName.add(videoFiles[i].getName());
videoPreName.add(getPreFileName(videoFiles[i].getName()));
videoExtenName.add(getExtensionName(videoFiles[i].getName()));
}
}
}
return videoPath;
}
/**
* 获取文件扩展名
*
* @param filename
* @return String
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/**
* 获取去掉扩展名的文件名
*
* @param filename
* @return String
*/
public static String getPreFileName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
}