Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在检测args[0]是否为null或不是文本文件_Java_Exception Handling_Args - Fatal编程技术网

Java 正在检测args[0]是否为null或不是文本文件

Java 正在检测args[0]是否为null或不是文本文件,java,exception-handling,args,Java,Exception Handling,Args,有人能看看下面的代码吗?如果args[0]为空,为什么它不显示错误消息?如果输入的文件重复,程序工作正常,显示文件已存在消息。Java向我抛出ArrayIndexOutOfBounds错误。我也尝试过将该异常添加到代码中,但没有任何结果 import java.io.*; import java.util.*; public class inputTest{ public static void main(String[] args) throws IOException{ if

有人能看看下面的代码吗?如果args[0]为空,为什么它不显示错误消息?如果输入的文件重复,程序工作正常,显示文件已存在消息。Java向我抛出ArrayIndexOutOfBounds错误。我也尝试过将该异常添加到代码中,但没有任何结果

import java.io.*;
import java.util.*;
public class inputTest{
    public static void main(String[] args) throws IOException{
    if(args.length() == 0){
    System.out.println("Please enter a correct text file name!");
    System.exit(1);
    }
    java.io.File file = new java.io.File(args[0]);
    if (file.exists()){
        System.out.println("This file already exists!"); // If file exists, throw exception
        System.exit(1);
    }
    // Create a file    
    java.io.PrintWriter output = new java.io.PrintWriter(file);

    }
}

另外,是否有办法确保在命令提示符下输入的文件是.txt格式的?

此代码将出现编译问题

无法对数组类型字符串[]调用length()

改为

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    if(args.length == 0){
        System.out.println("Please enter a correct text file name!");
        System.exit(1);
    }
    java.io.File file = new java.io.File(args[0]);
    if (file.exists()){
        System.out.println("This file already exists!"); // If file exists, throw exception
        System.exit(1);
    }
    // Create a file    

    java.io.PrintWriter output = new java.io.PrintWriter(file);
}

考虑使用apachecommons isEmpty()测试数组的空性,而不用担心处理空数组和空数组

您还可以使用Apache Commons IO getExtension()获取文件的扩展名


虽然这不能帮助您掌握Java的概念,但您现在可能已经熟悉Java开发人员使用的一组主要库,以及如何将库添加到项目中。

您如何调用它以将args[0]作为空字符串?
args[0]
不能为
null
。它可以抛出一个
ArrayIndexOutOfBoundsException
,也可以实际包含一些内容。@Sotirios它正在抛出一个ArrayIndexOutOfBoundsException,我不知道如何捕获它。线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:0是我使用上述代码时得到的。我尝试将ArrayIndexOutOfBoundsException添加到我的抛出行中,但仍然没有任何结果。@ch4dr0x use
args.length==0
则如果大小为0,控件将不会转到出现此异常的行。这是我当前拥有的:
public static void main(String[]args)抛出IOException{java.io.File=new java.io.File(args[0]);if(args.length==0){System.out.println(“正确用法是:java程序文件名”);System.exit(0);}
@ch4dr0x查看我刚才添加到answerQuestion的输出,当您将文件放入“try”中时块,它将它与我的其他代码隔离开来。我很少有使用try/catch的经验,但是如果我有“output.println”代码,我会把这些都放在try块中吗?我仍然得到这个该死的ArrayIndexOutOfBoundsException。这让我大吃一惊。