想了解java.io.FileNotFoundException:系统找不到指定的文件的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java.io.ioexception系统找不到指定的路
想了解java.io.FileNotFoundException:系统找不到指定的文件的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java.io.ioexception系统找不到指定的路径的相关问题,此外,我们还将为您介绍关于android – java.io.FileNotFoundException:/file/path.jpg open failed:ENOENT(没有这样的文件或目录)、FileNotFoundError: [Errno 2] 没有那个文件或目录 | FileNotFoundError: [WinError 2] 系统找不到指定的文件:、FileNotFoundError: [WinError 2] 系统找不到指定的文件、FileNotFoundError: [WinError 2] 系统找不到指定的文件。 解决方案的新知识。
本文目录一览:- java.io.FileNotFoundException:系统找不到指定的文件(java.io.ioexception系统找不到指定的路径)
- android – java.io.FileNotFoundException:/file/path.jpg open failed:ENOENT(没有这样的文件或目录)
- FileNotFoundError: [Errno 2] 没有那个文件或目录 | FileNotFoundError: [WinError 2] 系统找不到指定的文件:
- FileNotFoundError: [WinError 2] 系统找不到指定的文件
- FileNotFoundError: [WinError 2] 系统找不到指定的文件。 解决方案
java.io.FileNotFoundException:系统找不到指定的文件(java.io.ioexception系统找不到指定的路径)
如何解决java.io.FileNotFoundException:系统找不到指定的文件?
直接将word.txt作为项目根文件夹的子级和src的对等方放置
Project_Root
src
word.txt
免责声明:我想解释一下为什么这种方法适用于这种特殊情况,以及为什么它可能不适用于其他情况。
使用File
或任何其他FileXxx
变体时,你正在文件系统上相对于“工作目录”查找文件。工作目录,可以这样描述:
当你从命令行运行时
C:\EclipseWorkspace\ProjectRoot\bin > java com.mypackage.Hangman1
工作目录为C:\EclipseWorkspace\ProjectRoot\bin
。使用你的IDE(至少是我使用过的所有IDE),工作目录为ProjectRoot
。因此,当文件位于中时ProjectRoot
,仅使用文件名作为相对路径是有效的,因为它位于工作目录的根目录中。
同样,如果这是你的项目结构ProjectRoot\src\word.txt
,则该路径"src/word.txt"
将有效。
首先,工作目录可以随时更改。例如,像上面的示例一样从命令行运行代码,工作目录为bin。所以在这种情况下它将失败,因为没有bin\word.txt
其次,如果要将项目导出到jar中,并且文件被配置为包含在jar中,则该文件也将失败,因为该路径也将不再有效。
话虽如此,你需要确定文件是否是嵌入式资源(或仅仅是“资源”-有时我会互换使用的术语)。如果是这样,那么你将需要将文件构建到类路径中,并通过URL访问它。在这种情况下,你需要做的第一件事是确保将文件构建到类路径中。将文件放在项目根目录中,你必须配置构建以包括该文件。但是,如果将文件放在下面的src或目录中,则默认构建应将其放在类路径中。
你可以通过多种方式访问类路径资源。你可以利用Class
具有getResourceXxx
方法的类,从中获取类路径资源。
例如,如果将项目结构更改为ProjectRoot\src\resources\word.txt
,则可以使用以下命令:
InputStream is = Hangman1.class.getResourceAsstream("/resources/word.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
getResourceAsstream
返回一个InputStream
,但在后台获取一个URL。或者,URL如果需要的话,你可以得到一个。getResource()
将返回一个URL
对于Maven用户,目录结构类似于src/main/resources,resources
文件夹的内容放在类路径的根目录下。因此,如果其中有文件,则只能使用getResourceAsstream("/thefile.txt")
解决方法
我有一个名为“ word.txt”
的文件。
它与我的java
文件位于同一目录中。
但是,当我尝试通过以下代码访问它时,找不到此文件,发生错误:
Exception in thread "main" java.io.FileNotFoundException: word.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 Hangman1.main(Hangman1.java:6)
这是我的代码:
import java.io.File;
import java.util.*;
public class Hangman1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("word.txt"));
String in = "";
in = input.nextLine();
}
}
android – java.io.FileNotFoundException:/file/path.jpg open failed:ENOENT(没有这样的文件或目录)
private void saveImage() throws IOException { // Todo Auto-generated method stub InputStream header = new FileInputStream("/ClashMMA/res/drawable-hdpi/beatdown.jpg"); ParseFile file = new ParseFile(toByteArray(header)); try{ file.save(); } catch (ParseException e) { e.printstacktrace(); } ParSEObject displayImage = new ParSEObject("displayImage"); displayImage.put("header",file); try{ displayImage.save(); } catch (ParseException e1){ e1.printstacktrace(); } } private byte[] toByteArray(InputStream header) throws IOException { // Todo Auto-generated method stub ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int l; byte[] data = new byte[1024]; while ((l = header.read(data,data.length)) != -1) { buffer.write(data,l); } buffer.flush(); return buffer.toByteArray(); }
而我的错误是这样的;
java.io.FileNotFoundException:/ClashMMA/res/drawable-hdpi/beatdown.jpg:open Failed:ENOENT(没有这样的文件或目录)
但我确信文件在那里,因为我去了我的文件目录(在eclipse中),右键单击,然后单击copy Qualified Name.这基本上复制了文件路径.我已经尝试了一些其他路径,例如关闭我的计算机,以及我的src文件夹.有人可以告诉我我做错了什么吗?为什么它不会读取文件,实际上它在那里?详细解释请.
解决方法
你基本上有两个选择:
>将它们放入APK存档中,以便您可以使用getSystemResourceAsstream加载它们:请参阅How to load image for Android ImageView from classpath?>将文件复制到Android文件系统中,以便FileInputStream可以找到它们.
FileNotFoundError: [Errno 2] 没有那个文件或目录 | FileNotFoundError: [WinError 2] 系统找不到指定的文件:
如何解决FileNotFoundError: [Errno 2] 没有那个文件或目录 | FileNotFoundError: [WinError 2] 系统找不到指定的文件:?
我正在尝试使用 Django 模板上传文件,但在 save_project
上我收到此回溯错误
FileNotFoundError: [WinError 2] The system cannot find the file specified: ''E:\\forefront-v1.0\\media\\uploads\\temp\\10\\Video%20Sensor%20-%2013370_1798858.mp4'' -> ''E:\\forefront-v1.0\\media/uploads/project-videos/Video%20Sensor%20-%2013370_1798858.mp4''
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "E:\forefront-v1.0\venv\lib\site-packages\django\core\handlers\exception.py",line 34,in inner
response = get_response(request)
File "E:\forefront-v1.0\venv\lib\site-packages\django\core\handlers\base.py",line 115,in _get_response
response = self.process_exception_by_middleware(e,request)
File "E:\forefront-v1.0\venv\lib\site-packages\django\core\handlers\base.py",line 113,in _get_response
response = wrapped_callback(request,*callback_args,**callback_kwargs)
File "E:\forefront-v1.0\venv\lib\site-packages\django\views\generic\base.py",line 71,in view
return self.dispatch(request,*args,**kwargs)
File "E:\forefront-v1.0\venv\lib\site-packages\django\contrib\auth\mixins.py",line 52,in dispatch
return super().dispatch(request,line 109,**kwargs)
File "E:\forefront-v1.0\venv\lib\site-packages\django\views\generic\base.py",line 97,in dispatch
return handler(request,**kwargs)
File "E:\forefront-v1.0\projects\views.py",line 2660,in post
project_id = self.save_project(
File "E:\forefront-v1.0\projects\views.py",line 2792,in save_project
video1 = self.upload_video_locally(video1) if video1 else None
File "E:\forefront-v1.0\projects\views.py",line 2872,in upload_video_locally
shutil.move(temp_file_path,saved_file_path)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\python39\lib\shutil.py",line 826,in move
copy_function(src,real_dst)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\python39\lib\shutil.py",line 435,in copy2
copyfile(src,dst,follow_symlinks=follow_symlinks)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\python39\lib\shutil.py",line 264,in copyfile
with open(src,''rb'') as fsrc,open(dst,''wb'') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: ''E:\\forefront-v1.0\\media\\uploads\\temp\\10\\Video%20Sensor%20-%2013370_1798858.mp4''
[26/Jul/2021 13:12:27] "POST /upload/ HTTP/1.1" 500 23208
在项目 settings.py
# Upload
MEDIA_URL = ''/media/''
MEDIA_ROOT = os.path.join(BASE_DIR,''media'')
这是有问题的函数。在views.py
def save_project(
self,image,name,excerpt,body,caption1,caption2,description1,description2,image1,image2,video,video1,video2,authors_arr,paperlink,):
video = self.upload_video_locally(video) if video else None
video1 = self.upload_video_locally(video1) if video1 else None
video2 = self.upload_video_locally(video2) if video2 else None
if self.request.POST.get("previewed") or video is not None:
image = self.upload_file_locally(image,"banner",1) if image else None
else:
image = self.upload_file_locally(image,0) if image else None
if video1 is not None:
image1 = self.upload_file_locally(image1,"upm-img1",1) if image1 else None
else:
image1 = self.upload_file_locally(image1,0) if image1 else None
if video2 is not None:
image2 = self.upload_file_locally(image2,"upm-img2",1) if image2 else None
else:
image2 = self.upload_file_locally(image2,0) if image2 else None
# video2 = self.upload_video_locally(video2) if video1 else None
# image2 = self.upload_file_locally(image2,''upm-img2'',0) if image2 else None
# image3 = self.upload_file_locally(image3,''upm-img3'',0) if image3 else None
self.upload_file_to_s3() # upload all files to amazon s3
# self.upload_file_locally(image,True)
save_project = Project(
image=image,name=name,excerpt=excerpt,body=body,caption1=caption1,caption2=caption2,description1=description1,description2=description2,image1=image1,image2=image2,video=video,video1=video1,video2=video2,paperLink=paperlink,)
save_project.save()
# getting authors from request session
login_user_status = 0
for author in authors_arr:
if self.request.user.customuser.member.id == int(author["id"]):
login_user_status = 1
get_author = Member.objects.get(id=int(author["id"]))
save_project.authors.add(get_author)
if login_user_status == 0:
get_author = Member.objects.get(id=self.request.user.customuser.member.id)
save_project.authors.add(get_author)
return save_project.id
所以请帮我找到可能是问题的解决方案,我们可以解决这个问题。 我相信
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
FileNotFoundError: [WinError 2] 系统找不到指定的文件
用Idle运行Python脚本的时候发现如下错误:
Traceback (most recent call last):
File "D:\Python\Python36-32\lib\site-packages\selenium-3.4.3-py3.6.egg\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "D:\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "D:\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
后来在stackoverflow上找到如下解决办法:
In Windows , to use echo in subprocess, you would need to use shell=True . This is because echo is not a separate executable, but rather a built-in command for the windows command line. Example -
在windows 在子进程中使用echo,需要设置 shell =True,因为 echo 不是单独的命令,而是window CMD 内置的命令
process1 = subprocess.Popen(command1,stdout=subprocess.PIPE,shell=True)
Also, please do note, you should only use shell=True when absolutely necessary (as in this case , to use echo in windows in subprocess).
并且,需要注意,只有在绝对需要的情况下才使用shell=True
找到 subprocess.py脚本,将对应行加上shell=true 参数。
FileNotFoundError: [WinError 2] 系统找不到指定的文件。 解决方案
用 Idle 运行 Python 脚本的时候发现如下错误:
Traceback (most recent call last): File "C:\Users\DangKai\Desktop\pythonUI-unittest-selenium\venv\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start stdin=PIPE) File "D:\Software\Python37\lib\subprocess.py", line 756, in init restore_signals, start_new_session) File "D:\Software\Python37\lib\subprocess.py", line 1155, in _execute_child startupinfo) FileNotFoundError: [WinError 2] 系统找不到指定的文件。
后来在 stackoverflow 上找到如下解决办法:
In Windows , to use echo in subprocess, you would need to use shell=True . This is because echo is not a separate executable, but rather a built-in command for the windows command line. Example - 在 windows 在子进程中使用 echo,需要设置 shell =True,因为 echo 不是单独的命令,而是 window CMD 内置的命令
process1 = subprocess.Popen(command1,stdout=subprocess.PIPE,shell=True) Also, please do note, you should only use shell=True when absolutely necessary (as in this case , to use echo in windows in subprocess).
并且,需要注意,只有在绝对需要的情况下才使用 shell=True
找到 subprocess.py 脚本,将对应行加上 shell=true 参数
今天关于java.io.FileNotFoundException:系统找不到指定的文件和java.io.ioexception系统找不到指定的路径的分享就到这里,希望大家有所收获,若想了解更多关于android – java.io.FileNotFoundException:/file/path.jpg open failed:ENOENT(没有这样的文件或目录)、FileNotFoundError: [Errno 2] 没有那个文件或目录 | FileNotFoundError: [WinError 2] 系统找不到指定的文件:、FileNotFoundError: [WinError 2] 系统找不到指定的文件、FileNotFoundError: [WinError 2] 系统找不到指定的文件。 解决方案等相关知识,可以在本站进行查询。
本文标签: