我必须在哪里复制Netbeans 7.1 for java代码中的default.properties文件?
我正在尝试谷歌的一个代码,它允许从gmail中检索电子邮件并存储到mysql 当我启动代码时,它正在工作,但最后我收到一条错误消息: 无法连接到数据库 这很正常,因为我不知道如何创建default.properties文件: 我知道内容,但文件的格式必须是什么,我必须把这个文件放在哪里,在Netbeans中 我必须创建一个文件夹吗? 哪种文件格式的txt,java?? 文件名将为default.properties 代码可以在这里找到:sakthimaharai.hubpages.com 我需要帮助 求你了我必须在哪里复制Netbeans 7.1 for java代码中的default.properties文件?,java,Java,我正在尝试谷歌的一个代码,它允许从gmail中检索电子邮件并存储到mysql 当我启动代码时,它正在工作,但最后我收到一条错误消息: 无法连接到数据库 这很正常,因为我不知道如何创建default.properties文件: 我知道内容,但文件的格式必须是什么,我必须把这个文件放在哪里,在Netbeans中 我必须创建一个文件夹吗? 哪种文件格式的txt,java?? 文件名将为default.properties 代码可以在这里找到:sakthimaharai.hubpages.com 我需要
谢谢我不知道代码试图从哪里加载属性,您没有说太多,但是在正常情况下,文件应该在项目的类路径中 属性文件(
.properties
)如下所示:
key=value
key2=value2
#comment1
请提供更多信息,以便我们为您提供帮助。在Netbeans中,您可以使用上下文菜单创建属性文件来创建新元素,如所示。请小心输入
default
作为名称,因为NB会将.properties
添加到您编写的任何内容中,并且可能以default.properties.properties
结尾
最常见的是从类路径或工作目录中读取属性文件,在第一种情况下,您应该在源文件夹的根目录中创建该文件。在第二种情况下,您可以直接在项目节点中创建该文件,但在这种情况下,如果您想分发程序,则不会将该文件添加到最终的jar/war中
(甚至还有一些用于处理属性文件的代码)。好的,这是在谷歌中找到的完整代码,作者今天删除了这些代码,因此任何人都可以免费使用
package inboxreader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.HashSet;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Address;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
public class InboxReader {
static HashSet<String> mails;
public static void main(String args[])
{
while(true)
{
try {
System.out.println("Started.......");
Start();
System.out.println("...Read completed.......");
try {
Thread.sleep(1000*60*5);
} catch (InterruptedException e1) {
}
} catch (Exception e) {
try {connecttoMySql();
e.printStackTrace();
System.out.println("..Error in connection Sleeping...");
} catch (Exception e1) {
}
}
}
}
public static void Start() throws Exception {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "email@gmail.com", "password");
System.out.println(store);
int cout=0;
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
Message messages[] = inbox.getMessages();
for(Message message:messages) {
mails=new HashSet<String>();
System.out.println("Reading:"+ (messages.length-cout));
cout++;
InboxReader.storeAddresses(message);
dumpPart(message);
for(String temp:mails)
System.out.println(temp);
connecttoMySql();
message.setFlag(Flags.Flag.DELETED, true);
}
} catch (NoSuchProviderException e) {
connecttoMySql();
e.printStackTrace();
} catch (MessagingException e) {
connecttoMySql();
e.printStackTrace();
}
}
public static void storeAddresses(Message msg)
{
try {
for(Address adr:msg.getAllRecipients())
{
addAddresses(adr.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addAddresses(String input_text)
{
Pattern p= Pattern.compile("[A-Z0-9\\._%\\+\\-]+@[A-Z0-9\\.\\-]+\\.[A-Z]{2,4}",Pattern.CASE_INSENSITIVE);
Matcher m=p.matcher(input_text);
while(m.find())
{
mails.add(m.group());
}
}
public static void dumpPart(Part p) throws Exception {
if (p.isMimeType("text/plain")) {
try{
addAddresses((String)p.getContent());
}catch(Exception e){}
} else {
MimeMultipart mb = null;
try{
mb=(MimeMultipart ) (p.getContent());
}
catch(Exception e)
{ try{
if(p.getContent() instanceof String)
addAddresses((String)p.getContent());
}catch(Exception e1){}
return;
}
MimeBodyPart mb1=(MimeBodyPart) mb.getBodyPart(0);
mb1.saveFile("emailtext.html");
BufferedReader br = new BufferedReader(new FileReader("emailtext.html"));
StringBuffer content = new StringBuffer();
String line ="";
while((line = br.readLine())!= null )
{
if(line.length()>=2)if(line.substring(line.length()-1).equals("="))
{
content.append(line.substring(line.length()-1) );
}else
content.append(line+"\n");
}
addAddresses(content.toString());
}
}
public static void connecttoMySql()
{
Connection conn = null;
try
{
Properties details= new Properties();
details.load(new FileInputStream("details.properties"));
String userName = details.getProperty("root");
String password = details.getProperty("password_of-mysql");
String url = details.getProperty("jdbc:mysql://localhost/Test");
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
PreparedStatement st= conn.prepareStatement("insert into `Email_list` values(?)");
for(String mail:mails)
{
try{
st.setString(1, mail);
st.execute();
}catch(Exception e){}
}
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
e.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { }
}
}
}
}
并创建文件default.properties.properties,如Madth3示例中所述
多谢各位
默认的.properties.properties为:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class App
{
public static void main( String[] args )
{
Properties prop = new Properties();
try {
//set the properties value
prop.setProperty("database", "localhost");
prop.setProperty("dbuser", "root");
prop.setProperty("dbpassword", "password");
//save properties to project root folder
prop.store(new FileOutputStream("default.properties.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
您如何尝试读取
default.properties
?请粘贴一些代码,以便人们可以帮助你。Tichodroma说的+1:提供一些(相关)代码。。。
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class App
{
public static void main( String[] args )
{
Properties prop = new Properties();
try {
//set the properties value
prop.setProperty("database", "localhost");
prop.setProperty("dbuser", "root");
prop.setProperty("dbpassword", "password");
//save properties to project root folder
prop.store(new FileOutputStream("default.properties.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}