Java 为什么程序找不到指定的系统文件以及如何修复它?

Java 为什么程序找不到指定的系统文件以及如何修复它?,java,file,file-io,network-programming,smtp,Java,File,File Io,Network Programming,Smtp,我想测试这个程序,但我不明白为什么它不起作用。它说找不到file.txt,有什么原因吗?我只想让file.txt读取来自客户端和服务器的日志协议命令消息,将它们附加到一个名为“file.txt”的新文件中。它表示找不到指定的文件。当我在同一路径中手动创建文件时,它会工作,但除此之外,它不会工作。我希望它自动创建新文件并附加日志消息,仅此而已 错误: Exception in thread "main" java.io.FileNotFoundException: C:\Users\Jaime\D

我想测试这个程序,但我不明白为什么它不起作用。它说找不到file.txt,有什么原因吗?我只想让file.txt读取来自客户端和服务器的日志协议命令消息,将它们附加到一个名为“file.txt”的新文件中。它表示找不到指定的文件。当我在同一路径中手动创建文件时,它会工作,但除此之外,它不会工作。我希望它自动创建新文件并附加日志消息,仅此而已

错误:

Exception in thread "main" java.io.FileNotFoundException: C:\Users\Jaime\Desktop\file.txt (The system cannot find the file specified)
 at java.io.FileInputStream.open(Native Method)
 at java.io.FileInputStream.<init>(FileInputStream.java:138)
 at java.io.FileInputStream.<init>(FileInputStream.java:97)
 at java.io.FileReader.<init>(FileReader.java:58)
 at SMTPDemo.main(SMTPDemo.java:28)
线程“main”java.io.FileNotFoundException中的异常:C:\Users\Jaime\Desktop\file.txt(系统找不到指定的文件) 在java.io.FileInputStream.open(本机方法) 位于java.io.FileInputStream。(FileInputStream.java:138) 位于java.io.FileInputStream。(FileInputStream.java:97) 位于java.io.FileReader。(FileReader.java:58) 位于SMTPDemo.main(SMTPDemo.java:28)
代码:

/**
资料来源:http://www.java2s.com/Code/Java/Network-Protocol/SendingMailUsingSockets.htm
*/
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.OutputStream;
导入java.io.OutputStreamWriter;
导入java.io.PrintWriter;
导入java.net.InetAddress;
导入java.net.Socket;
导入java.net.UnknownHostException;
公共类SMTPDemo
{
公共静态void main(字符串args[])引发IOException,UnknownHostException
{
String msgFile=“C:\\Users\\Jaime\\Desktop\\file.txt”;
字符串from=”java2s@java2s.com";
字符串to=”yourEmail@yourServer.com";
字符串mailHost=“localhost”;//从您的主机更改为localhost
SMTP邮件=新SMTP(邮件主机);
如果(邮件!=null)
{
if(mail.send(新文件读取器(msgFile)、from、to))
{
System.out.println(“邮件已发送”);
} 
其他的
{
System.out.println(“连接到SMTP服务器失败!”);
}
}
System.out.println(“完成”);
}
静态类SMTP
{
私有最终静态int SMTP_PORT=31000;//从25更改为31000
邮件主机地址;
本地主机地址;
缓冲读取器;
打印输出;
公共SMTP(字符串主机)引发UnknownHostException
{
mailHost=InetAddress.getByName(主机);
localhost=InetAddress.getLocalHost();
System.out.println(“mailhost=“+mailhost”);
System.out.println(“localhost=“+localhost”);
System.out.println(“SMTP构造函数完成\n”);
}
公共布尔发送(FileReader msgFileReader、String from、String to)引发IOException
{
承插短管;
InputStream酒店;
输出流输出;
缓冲阅读味精;
msg=新的BufferedReader(msgFileReader);
smtpPipe=新套接字(邮件主机、SMTP_端口);
如果(smtpPipe==null)
{
返回false;
}
inn=smtpPipe.getInputStream();
outt=smtpPipe.getOutputStream();
in=新的BufferedReader(新的InputStreamReader(inn));
out=新的PrintWriter(新的OutputStreamWriter(outt)),true;
如果(inn==null | | outt==null)
{
System.out.println(“未能将流打开到套接字”);
返回false;
}
字符串initialID=in.readLine();
System.out.println(initialID);
System.out.println(“HELO”+localhost.getHostName());
out.println(“HELO”+localhost.getHostName());
字符串welcome=in.readLine();
System.out.println(欢迎);
System.out.println(“邮件发件人:”);
out.println(“邮件发件人:”);
字符串senderOK=in.readLine();
系统输出打印LN(senderOK);
System.out.println(“RCPT TO:”);
out.println(“RCPT TO:”);
字符串recipientOK=in.readLine();
System.out.println(recipientOK);
系统输出打印项次(“数据”);
out.println(“数据”);
弦线;
而((line=msg.readLine())!=null)
{
out.println(行);
}
系统输出打印项次(“.”);
out.println(“.”);
字符串acceptedOK=in.readLine();
System.out.println(acceptedOK);
System.out.println(“退出”);
out.println(“退出”);
返回true;
}
}
}

使用不存在的文件创建
文件读取器
将始终引发
FileNotFoundException
。JVM通常只在您计划使用writer写入文件时为您创建一个文件


上面的代码对我来说没有多大意义,但如果您想在第一次运行此程序时不考虑存在的文件而运行它,请参阅
String msgFile=“file.txt”将文件放在可复制的路径中(例如
user.home
的子目录)。我在线程“main”java.io.FileNotFoundException中尝试了异常:C:\Users\Jaime\Desktop\file.txt(系统找不到指定的文件)
C:\Users\Jaime\Desktop\file.txt
好吗?文件是否实际位于本地文件系统上的该位置?作为检查,您可以在代码
file f=new file(filePathString)中输入以下内容:
如果(f.exists()){/*做点什么*/}
/**
   Source: http://www.java2s.com/Code/Java/Network-Protocol/SendingMailUsingSockets.htm
*/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SMTPDemo 
{
   public static void main(String args[]) throws IOException, UnknownHostException 
   {
     String msgFile = "C:\\Users\\Jaime\\Desktop\\file.txt";
     String from = "java2s@java2s.com";
     String to = "yourEmail@yourServer.com";
     String mailHost = "localhost"; //changed from yourHost to localhost
     SMTP mail = new SMTP(mailHost);
     if(mail != null) 
     {
       if(mail.send(new FileReader(msgFile), from, to)) 
       {
         System.out.println("Mail sent.");
       } 
       else 
       {
         System.out.println("Connect to SMTP server failed!");
       }
     }
     System.out.println("Done.");
   }

   static class SMTP 
   {
     private final static int SMTP_PORT = 31000; //changed from 25 to 31000

     InetAddress mailHost;

     InetAddress localhost;

     BufferedReader in;

     PrintWriter out;

     public SMTP(String host) throws UnknownHostException 
     {
       mailHost = InetAddress.getByName(host);
       localhost = InetAddress.getLocalHost();
       System.out.println("mailhost = " + mailHost);
       System.out.println("localhost= " + localhost);
       System.out.println("SMTP constructor done\n");
     }

     public boolean send(FileReader msgFileReader, String from, String to) throws IOException 
     {
       Socket smtpPipe;
       InputStream inn;
       OutputStream outt;
       BufferedReader msg;
       msg = new BufferedReader(msgFileReader);
       smtpPipe = new Socket(mailHost, SMTP_PORT);
       if(smtpPipe == null) 
       {
         return false;
       }
       inn = smtpPipe.getInputStream();
       outt = smtpPipe.getOutputStream();
       in = new BufferedReader(new InputStreamReader(inn));
       out = new PrintWriter(new OutputStreamWriter(outt), true);
       if(inn == null || outt == null) 
       {
         System.out.println("Failed to open streams to socket.");
         return false;
       }
       String initialID = in.readLine();
       System.out.println(initialID);
       System.out.println("HELO " + localhost.getHostName());
       out.println("HELO " + localhost.getHostName());
       String welcome = in.readLine();
       System.out.println(welcome);
       System.out.println("MAIL From:<" + from + ">");
       out.println("MAIL From:<" + from + ">");
       String senderOK = in.readLine();
       System.out.println(senderOK);
       System.out.println("RCPT TO:<" + to + ">");
       out.println("RCPT TO:<" + to + ">");
       String recipientOK = in.readLine();
       System.out.println(recipientOK);
       System.out.println("DATA");
       out.println("DATA");
       String line;
       while ((line = msg.readLine()) != null) 
       {
         out.println(line);
       }
       System.out.println(".");
       out.println(".");
       String acceptedOK = in.readLine();
       System.out.println(acceptedOK);
       System.out.println("QUIT");
       out.println("QUIT");
       return true;
     }
  }
}