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

标签 java file file-io network-programming smtp

我想测试这个程序,但我不明白为什么它不起作用。它说找不到 file.txt,有什么原因吗?我只是想让 file.txt 从客户端和服务器读取日志协议(protocol)命令消息,将它们附加到一个名为“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)
<小时/>

代码:

/**
   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;
     }
  }
}

最佳答案

使用不存在的文件创建FileReader将始终抛出FileNotFoundException。如果您打算使用写入器写入文件,JVM 通常只会为您创建一个文件。

上面的代码对我来说没有多大意义,但是如果您希望它在第一次运行该程序时无论文件是否存在都运行,请参见 File.createNewFile()

关于java - 为什么程序找不到指定的系统文件以及如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17792021/

相关文章:

java - 如何使用 if 语句正确格式化文件 .txt 中数组列表的日期

c++ - 以 fstream::out 模式打开 fstream 是否会删除其当前内容?

Java 小服务程序 : "best practice" for running background tasks?

java - 计算 myClickHandler 函数不起作用

c - C语言从文本文件中读取数据

c - 如何用 C 将二进制位写入二进制文件?

python - 读取同一行中多个项目的项目

java - 没看懂包含NodeList的程序流程

java - FirebaseInstanceId.getInstance().getInstanceId() 在哪些情况下会失败?

java - 循环遍历 csv 文件时出现奇怪的行