使用番石榴的基础知识 [英] The very basics for using Guava

查看:119
本文介绍了使用番石榴的基础知识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全谷歌了。但我发现的一切都是关于我过去的下一步。



我在很长一段时间后回到编程,并学习Java(& OO)第一次。我问过要读一个文件,有人建议我不要试图学习Java的扭曲,而是使用Guava。谷歌搜索后,我大致发现了番石榴是什么和做了什么(甜蜜!)我下载了它。



然后我发现我实际上并不知道使用番石榴 意思是



我使用过导入(比如junit,还有基本类,比如,我不知道,数学和东西),我希望它会类似。但我不知道,例如,什么文件夹放入Guava,也不知道Eclipse是否会识别它。 (也许这是我唯一需要知道的事情?我也不知道。)



我在这里要求的是:我如何从意识中走出来一个新技术(即Guava库)在Eclipse中使用该技术编写代码并执行?

解决方案

番石榴样本用法



是的,使用Guava是一个好主意,特别是对于简单的任务,如读取文件

 字符串内容= Files.toString(文件,Charsets.UTF_8)


  BufferedReader in = null ; 
StringBuilder sb = new StringBuilder();
try {
in = new BufferedReader(new FileReader(file));
String str;
while((str = in.readLine())!= null){
sb.append(str).append('\ n');
}
} catch(IOException e){
//在这里做点什么
}最后{
//这也应该在try / catch块$中b $ b if(in!= null)in.close();
}
返回sb.toString();

(但实际上你需要多年开发Java才能真正体会到恕我直言。)



配置Eclipse使用Guava



在Eclipse中处理这样一个库的最简单方法是创建一个用户库并将用户库添加到项目中(右键单击项目:构建路径 - >添加库 - >用户库)。



以下是生成用户库的简短指南:


  1. 打开菜单项首选项 - > Java - >构建路径 - >用户库

  2. 新建... ,输入名称(例如'Guava')

  3. 添加JAR ... 并搜索您下载的JAR文件

    (解压缩番石榴档案事先,在里面你会找到文件 guava- r07.jar 。在此对话框中选择,最好在将其移至更理想的位置后)

  4. 现在选择来源附件 - > ;编辑... ,找到文件的路径 guava-src-r07

  5. 如果您希望,您可以为JavaDoc位置重复此过程,将其指向番石榴分发中的 javadoc 文件夹

  6. 确定

现在你应该全部设置,你可以选择用户库并将其添加到您选择的项目中。



使用Maven



为了完整性:成为依赖的拥护者使用maven管理我当然会建议使用 maven m2eclipse 而不是用户库。


I totally did google this. But everything I found is about the next step past where I am.

I am returning to programming after a long time away, and learning Java (& OO) for the first time. I asked about reading a file, and someone suggested I not try to learn Java's contortions for this, but instead use Guava. After googling, I found out roughly what Guava is and does (sweet!) and I downloaded it.

Then I discovered that I don't actually know what "use Guava" means.

I have used imports (like junit, plus basic classes like, I dunno, Math and stuff), and I expect it will be similar. But I don't know, for example, what folder to put Guava in, nor whether Eclipse will recognize it. (Maybe that's the only thing I need to know? I don't know that either.)

What I am asking for here is this: How do I go from awareness of a new tech (namely Guava libraries) to having code written using that tech, in Eclipse, and executing?

解决方案

Guava Sample Usage

Yes, using Guava is a great idea, especially for simple tasks as reading a file:

String contents = Files.toString(file, Charsets.UTF_8)

when compared to plain java versions like this:

BufferedReader in = null;
StringBuilder sb = new StringBuilder();
try {
    in = new BufferedReader(new FileReader(file));
    String str;
    while ((str = in.readLine()) != null) {
        sb.append(str).append('\n');
    }
} catch (IOException e) {
    // do something here
} finally{
    // and this should also be in a try / catch block
    if(in!=null)in.close();
}
return sb.toString();

(But actually you need to have developed Java for years to really appreciate the easiness IMHO.)

Configuring Eclipse to use Guava

The easiest way to deal with such a library in Eclipse is to create a User library and add the user library to your project (right click project: Build Path -> Add Library -> User Library).

Here is a short guide to generating a User Library:

  1. Open the menu entry Preferences -> Java -> Build Path -> User Libraries
  2. Press New..., enter a name (e.g. 'Guava')
  3. Press Add JARs... and search for the JAR files you downloaded
    (Unzip the Guava archive beforehand, inside you'll find the file guava-r07.jar. Select that in this dialog, preferably after moving it to a more desirable location)
  4. Now select Source Attachment -> Edit..., and find the path to the file guava-src-r07.
  5. If you want to, you can repeat this process for the JavaDoc location, pointing it to the javadoc folder from the guava distribution
  6. Press OK.

Now you should be all set, you can select the user library and add it to a project of your choice.

Using Maven

For completeness: being an advocate of dependency management with maven I would of course suggest to manage project dependencies with maven and m2eclipse instead of with user libraries.

这篇关于使用番石榴的基础知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆