GVKun编程网logo

线程“主”中的异常java.io.FileNotFoundException:错误(线程main java中的异常怎么解决)

9

在本文中,我们将带你了解线程“主”中的异常java.io.FileNotFoundException:错误在这篇文章中,我们将为您详细介绍线程“主”中的异常java.io.FileNotFoundEx

在本文中,我们将带你了解线程“主”中的异常java.io.FileNotFoundException:错误在这篇文章中,我们将为您详细介绍线程“主”中的异常java.io.FileNotFoundException:错误的方方面面,并解答线程main java中的异常怎么解决常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的ios – 线程“main”中的异常java.lang.RuntimeException:无效的十六进制字符:v – APNS notnoop、Java Appium-线程“主”中的异常java.lang.ExceptionInInitializerError-由以下原因引起:java.lang.IllegalArgumentException、java – 使用Exception类或FileNotFoundException类捕获异常之间的区别、java.io.FileNotFoundException: D:e (拒绝访问)

本文目录一览:

线程“主”中的异常java.io.FileNotFoundException:错误(线程main java中的异常怎么解决)

线程“主”中的异常java.io.FileNotFoundException:错误(线程main java中的异常怎么解决)

我正在使用Eclipse编译和运行我的Java代码。

这是我得到的错误。

Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified)    at java.io.FileInputStream.open(Native Method)    at java.io.FileInputStream.<init>(Unknown Source)    at java.util.Scanner.<init>(Unknown Source)    at helloworld.main(helloworld.java:9)

这是我的代码

import java.io.File;import java.io.IOException;import java.util.Scanner;public class helloworld {    public static void main(String[] args) throws IOException {        Scanner KB = new Scanner(new File("file.txt"));        while (KB.hasNext()) {            String line = KB.nextLine();            System.out.println(line);        }    }}

File.txt
我在项目的同一文件夹中创建了file.txt。

答案1

小编典典

您的文件应直接位于项目文件夹下,而不应位于其他任何子文件夹内。

因此,如果您的项目文件夹为MyProject,则其文件夹结构(虽然不完整)应类似于:-

MyProject +- src +          |      |          |      +-- Your source file          +- file.txt

它不应该是under src文件夹。


或者,您可以提供以下相对于项目文件夹的路径,以在以下位置搜索文件src folder:-

new File("src/file.txt");

ios – 线程“main”中的异常java.lang.RuntimeException:无效的十六进制字符:v – APNS notnoop

ios – 线程“main”中的异常java.lang.RuntimeException:无效的十六进制字符:v – APNS notnoop

我一直在尝试实现notnoop APNS项目,以便向iOS设备发送推送通知,但我通过控制台收到此输出:

Exception in thread “main” java.lang.RuntimeException: Invalid hex character: v
at com.notnoop.apns.internal.Utilities.charVal(Utilities.java:133)
at com.notnoop.apns.internal.Utilities.decodeHex(Utilities.java:120)
at com.notnoop.apns.EnhancedApnsNotification.(EnhancedApnsNotification.java:72)
at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:54)
at com.notnoop.apns.internal.ApnsServiceImpl.push(ApnsServiceImpl.java:36)
at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:45)
at PushServiceTryout.main(PushServiceTryout.java:16)

我有什么不对的想法吗?我能感觉到我快到了!提前致谢.

编辑:这是代码.无论如何,一旦我运行程序,我就会收到错误:s

import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;

public class PushServiceTryout
{
    public static void main(String[] args)
    {
        ApnsService service = APNS.newService()
                .withCert("c:/fcertificates.p12","1234")
                .withSandBoxDestination()
                .build();
        String msg = "Hello";

        String payload = APNS.newPayload().alertBody(msg).build();
        String token = "devicetoken";
        service.push(token,payload);
    }
}

解决方法

这是错误:

String token = "devicetoken";

您不能将字符串“devicetoken”用作设备令牌.设备令牌由64个十六进制字符组成(例如:“1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef”).当然,你也不能使用随机十六进制字符串.您应该使用从安装了应用程序的设备发送给您的设备令牌.

您获得String中第一个非十六进制字符的异常,恰好是v.

Java Appium-线程“主”中的异常java.lang.ExceptionInInitializerError-由以下原因引起:java.lang.IllegalArgumentException

Java Appium-线程“主”中的异常java.lang.ExceptionInInitializerError-由以下原因引起:java.lang.IllegalArgumentException

如何解决Java Appium-线程“主”中的异常java.lang.ExceptionInInitializerError-由以下原因引起:java.lang.IllegalArgumentException?

我正在用Java学习Appium,并尝试通过安装构造函数应用pageObjects来将驱动程序传递给我的测试用例。当我执行代码时,出现以下错误,我在线搜索并找不到解决方案。请帮助我调查此问题。我是Java编程的新手。

Sep 17,2020 3:29:25 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
INFO: Detected dialect: W3C
Exception in thread "main" java.lang.ExceptionInInitializerError
    at io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy(ProxyFactory.java:52)
    at io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy(ProxyFactory.java:33)
    at io.appium.java_client.pagefactory.AppiumFieldDecorator.proxyForAnElement(AppiumFieldDecorator.java:222)
    at io.appium.java_client.pagefactory.AppiumFieldDecorator.access$0(AppiumFieldDecorator.java:220)
    at io.appium.java_client.pagefactory.AppiumFieldDecorator$1.proxyForLocator(AppiumFieldDecorator.java:105)
    at org.openqa.selenium.support.pagefactory.DefaultFieldDecorator.decorate(DefaultFieldDecorator.java:62)
    at io.appium.java_client.pagefactory.AppiumFieldDecorator.decorate(AppiumFieldDecorator.java:160)
    at org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:113)
    at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)
    at pageObjects.HomePage.<init>(HomePage.java:18)
    at TestCases.Basics.main(Basics.java:22)
Caused by: java.lang.IllegalArgumentException
    at org.objectweb.asm.ClassVisitor.<init>(ClassVisitor.java:79)
    at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:49)
    at net.sf.cglib.core.DefaultGeneratorStrategy.getClassVisitor(DefaultGeneratorStrategy.java:30)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
    at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:332)
    at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:96)
    at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:94)
    at net.sf.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
    at java.util.concurrent.FutureTask.run(UnkNown Source)
    at net.sf.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
    at net.sf.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
    at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:119)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:294)
    at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:174)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:153)
    at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:73)
    ... 11 more

HomePage.java代码:

package pageObjects;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;

public class HomePage{
    //All the objects belongs to one page will be defined in a java class
    //1. Call the driver object from testcase to PageObject file

    //Concatenate driver
    public HomePage(AppiumDriver driver) {
        // Todo Auto-generated constructor stub
        //AppiumFieldDecorator give the ability to test across iOS and Android platforms
        PageFactory.initElements(new AppiumFieldDecorator(driver),this);
    }

    @AndroidFindBy(xpath="//android.widget.TextView[@text=''Preference'']")
    public WebElement preferences;

}

Basics.java代码:

package TestCases;

import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import pageObjects.HomePage;
import pageObjects.PreferencesPage;

public class Basics extends Base{

    public static void main(String[] args) throws MalformedURLException {
    // Todo Auto-generated method stub

    AndroidDriver<AndroidElement> driver = Capabilities();
    /*  
     *  //xpath id className,androidUIautomator
     *  xpath Syntax
     *  //tagName[@attribute=''value'']
     */
    HomePage home = new HomePage(driver);
    home.preferences.click();
    //PreferencesPage preferences = new PreferencesPage(driver);
    driver.findElementByXPath("//android.widget.TextView[@text=''Preference'']").click();
    driver.findElementByXPath("//android.widget.TextView[@text=''3. Preference dependencies'']").click();
    driver.findElementById("android:id/checkBox").click();
    driver.findElementByXPath("(//android.widget.RelativeLayout)[2]").click();
    driver.findElementByClassName("android.widget.EditText").sendKeys("hello");
    driver.findElementsByClassName("android.widget.Button").get(1).click();
        
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

java – 使用Exception类或FileNotFoundException类捕获异常之间的区别

java – 使用Exception类或FileNotFoundException类捕获异常之间的区别

就像我有这两个场景我们必须处理FileNotFoundException

情况1:

try {
        FileInputStream fis = new FileInputStream("test1.txt");
    } catch (FileNotFoundException e) {
        e.printstacktrace();
    }

案例2:

try {
        FileInputStream fis = new FileInputStream("test1.txt");
    } catch (Exception e) {
        e.printstacktrace();
    }

在两种情况下,打印的Stack Trace都是相同的.我想知道两种实现之间的区别以及应该首选的内容?

解决方法

从 docs开始,它给出了原因:

“A subclass inherits all the members (fields,methods,and nested
classes) from its superclass. Constructors are not members,so they
are not inherited by subclasses,but the constructor of the superclass
can be invoked from the subclass.”

Exception类是所有其他异常类的父级.因此,如果您知道要获取FileNotFoundException,那么最好使用该异常.制作例外是一个通用的电话.

这有助于您理解:

因此,您可以看到Exception类处于更高层次结构,因此它意味着它将捕获除FileIOExcetion之外的任何异常.但是,如果要确保尝试打开由指定路径名表示的文件失败,则必须使用FileIOExcetion.

所以这是一个理想的方法应该是:

try {
      // Lets say you want to open a file from its file name.
    } catch (FileNotFoundException e) {
      // here you can indicate that the user specified a file which doesn't exist.
      // May be you can try to reopen file selection dialog Box.
    } catch (IOException e) {
      // Here you can indicate that the file cannot be opened.
    }

而相应的:

try {
  // Lets say you want to open a file from its file name.
} catch (Exception e) {
  // indicate that something was wrong
  // display the exception's "reason" string.
}

另请查看:Is it really that bad to catch a general exception?

java.io.FileNotFoundException: D:e (拒绝访问)

java.io.FileNotFoundException: D:e (拒绝访问)

这是因为访问的文件地址是文件夹而不是,具体的文件,把路径改为具体的文件就可以了。

 参考文章:https://blog.csdn.net/w405722907/article/details/78610485

 

/**
 * 查看某个文件夹下所有的文件  并删除文件
 */
@RestController
public class One {

    /**
     *查看某个文件夹下所有的文件
     * @param args
     */
    public static void main(String[] args) {
        /*File file1 = new File("D:\\e\\test.txt");
        delete(file1);
        File file = new File("D:" + File.separator);
        print(file);*/
        //"D:\\门头沟国高新2018年度(管委).xlsx"
        try {
            download("门头沟国高新2018年度(管委).xlsx");
        } catch (IOException e) {
            e.printstacktrace();
        }
       /* String s = "D:/e/D:/门头沟国高新2018年度(管委).xlsx (文件名、目录名或卷标语法不正确。)";
        String substring = s.substring(8,s.length());
        System.out.println(substring);*/
    }

    /**
     * 打印某个文件夹下的所有文件名字
     * @param file
     */
    public static void print(File file){
        if (file != null){
            if (file.isDirectory()){
                File[] files = file.listFiles();
                if (files != null){
                    for (int i = 0; i < files.length; i++){
                        System.out.println(files[i]);
                    }
                }
            }
        }
    }

    /**
     * 删除某个文件
     * @param file
     */
    public static void delete(File file){
        if (file != null && file.exists()){
            boolean delete = file.delete();
            System.out.println(delete);
        }
    }

    /**
     * 下载某个文件
     * @param fileName
     * @throws IOException
     */
    public static void download(String fileName) throws IOException {
        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;
        try {
             fileInputStream = new FileInputStream("D:\\" + fileName);
            fileOutputStream = new FileOutputStream("D:\\e\\" + fileName);
        } catch (FileNotFoundException e) {
            e.printstacktrace();
        }
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(fileOutputStream);
        byte[] bytes = new byte[1024];
        while (bufferedInputStream.read(bytes) != -1){
            bufferedoutputstream.write(bytes);
        }
        bufferedoutputstream.flush();
        bufferedoutputstream.close();
        bufferedInputStream.close();
    }
}

关于线程“主”中的异常java.io.FileNotFoundException:错误线程main java中的异常怎么解决的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于ios – 线程“main”中的异常java.lang.RuntimeException:无效的十六进制字符:v – APNS notnoop、Java Appium-线程“主”中的异常java.lang.ExceptionInInitializerError-由以下原因引起:java.lang.IllegalArgumentException、java – 使用Exception类或FileNotFoundException类捕获异常之间的区别、java.io.FileNotFoundException: D:e (拒绝访问)等相关知识的信息别忘了在本站进行查找喔。

本文标签: