Java String类中的常量池鱼intern()


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>理解String 及 String.intern() 在实际中的应用 - 任香980101 - ITeye技术网站</title>
<meta name="description" content="1. 首先String不属于8种基本数据类型,String是一个对象。   因为对象的默认值是null,所以String的默认值也是null;但它又是一种特殊的对象,有其它� ..." />
<meta name="keywords" content="Apache, Tomcat, JVM, performance 理解String 及 String.intern() 在实际中的应用" />
<link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon" />
<link rel="search" type="application/opensearchdescription+xml" href="/open_search.xml" title="ITeye" />
<link href="/rss" rel="alternate" title="任香980101" type="application/rss+xml" />
<link href="http://www.iteye.com/stylesheets/blog.css?1442202041" media="screen" rel="stylesheet" type="text/css" />
<link href="http://www.iteye.com/stylesheets/themes/blog/orange.css?1326191326" media="screen" rel="stylesheet" type="text/css" />
<script src="http://www.iteye.com/javascripts/application.js?1358214518" type="text/javascript"></script>
<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-535605-1']);
_gaq.push(['_setDomainName', 'iteye.com']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>


<link href="http://www.iteye.com/javascripts/syntaxhighlighter/SyntaxHighlighter.css?1348819953" media="screen" rel="stylesheet" type="text/css" />
<script src="http://www.iteye.com/javascripts/syntaxhighlighter/shCoreCommon.js?1325907333" type="text/javascript"></script>
<script src="http://www.iteye.com/javascripts/hotkey.js?1324994303" type="text/javascript"></script>
<script src="http://www.iteye.com/javascripts/code_favorites.js?1358214518" type="text/javascript"></script>
<script src="http://www.iteye.com/javascripts/weiboshare.js?1324994303" type="text/javascript"></script>
</head>
<body>
<div id="header">
<div id="blog_site_nav">
<a href="http://www.iteye.com/" class="homepage">首页</a>
<a href="http://www.iteye.com/news">资讯</a>
<a href="http://www.iteye.com/magazines">精华</a>
<a href="http://www.iteye.com/forums">论坛</a>
<a href="http://www.iteye.com/ask">问答</a>
<a href="http://www.iteye.com/blogs">博客</a>
<a href="http://www.iteye.com/blogs/subjects">专栏</a>
<a href="http://www.iteye.com/groups">群组</a>
<a href="#" οnclick="return false;" id="msna"><u>更多</u> <small>▼</small></a>
<div class="quick_menu" style="display:none;">
<a target="_blank" href="http://job.iteye.com/iteye">招聘</a>
<a href="http://www.iteye.com/search">搜索</a>
</div>
</div>

<div id="user_nav">
<a href="/login" class="welcome" title="登录">您还未登录 !</a>
<a href="/login">登录</a>
<a href="/signup" class="nobg">注册</a>
</div>

</div>

<div id="page">
<div id="branding" class="clearfix">
<div id="blog_name">
<h1><a href="/">任香980101</a></h1>
</div>
<div id='fd'></div>
<div id="blog_navbar">
<ul>
<li class='blog_navbar_for'><a href="http://renxiangzyq.iteye.com"><strong>博客</strong></a></li>
<li ><a href="/weibo">微博</a></li>
<li ><a href="/album">相册</a></li>
<li ><a href="/link">收藏</a></li>
<li ><a href="/blog/guest_book">留言</a></li>
<li ><a href="/blog/profile">关于我</a></li>
</ul>

<div class="search">
<form action="/blog/search" method="get">
<input class="search_text" id="query" name="query" style="margin-left: 10px;width: 110px;" type="text" value="" />
<input class="submit_search" type="submit" value="" />
</form>
</div>
<div id="fd"></div>
</div>
</div>
<div>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-6937372349244493";
google_ad_slot = "3775465988";
google_ad_width = 320;
google_ad_height = 100;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<div id="content" class="clearfix">
<div id="main">







<div class="h-entry" style='display:none'>
<a href="http://renxiangzyq.iteye.com" class="p-author" target="_blank">lich0079</a>
</div>


<div class="blog_main">
<div class="blog_title">
<h3>
<a href="/blog/549554">理解String 及 String.intern() 在实际中的应用</a>
<em class="actions"> </em>
</h3>
<ul class='blog_categories'><strong>博客分类:</strong> <li><a href="/category/84529">J2EE</a></li> </ul>
<div class='news_tag'><a href="http://www.iteye.com/blogs/tag/Apache">Apache</a><a href="http://www.iteye.com/blogs/tag/Tomcat">Tomcat</a><a href="http://www.iteye.com/blogs/tag/JVM">JVM</a><a href="http://www.iteye.com/blogs/tag/performance">performance</a> </div>
</div>

<div id="blog_content" class="blog_content">
<pre class="java" name="code">1. 首先String不属于8种基本数据类型,String是一个对象。

  因为对象的默认值是null,所以String的默认值也是null;但它又是一种特殊的对象,有其它对象没有的一些特性。

  2. new String()和new String(“”)都是申明一个新的空字符串,是空串不是null;

  3. String str=”kvill”;
String str=new String (“kvill”);的区别:

  在这里,我们不谈堆,也不谈栈,只先简单引入常量池这个简单的概念。

  常量池(constant pool)指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据。它包括了关于类、方法、接口等中的常量,也包括字符串常量。

  看例1:

String s0=”kvill”;
String s1=”kvill”;
String s2=”kv” + “ill”;
System.out.println( s0==s1 );
System.out.println( s0==s2 );

  结果为:

true
true

  首先,我们要知道Java会确保一个字符串常量只有一个拷贝。

  因为例子中的s0和s1中的”kvill”都是字符串常量,它们在编译期就被确定了,所以s0==s1为true;而”kv”和”ill”也都是字符串常量,当一个字符串由多个字符串常量连接而成时,它自己肯定也是字符串常量,所以s2也同样在编译期就被解析为一个字符串常量,所以s2也是常量池中”kvill”的一个引用。

  所以我们得出s0==s1==s2;

  用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间。

  看例2:

String s0=”kvill”;
String s1=new String(”kvill”);
String s2=”kv” + new String(“ill”);
System.out.println( s0==s1 );
System.out.println( s0==s2 );
System.out.println( s1==s2 );

  结果为:

false
false
false

  例2中s0还是常量池中”kvill”的应用,s1因为无法在编译期确定,所以是运行时创建的新对象”kvill”的引用,s2因为有后半部分new String(“ill”)所以也无法在编译期确定,所以也是一个新创建对象”kvill”的应用;明白了这些也就知道为何得出此结果了。

  4. String.intern():

  再补充介绍一点:存在于.class文件中的常量池,在运行期被JVM装载,并且可以扩充。String的intern()方法就是扩充常量池的一个方法;当一个String实例str调用intern()方法时,Java查找常量池中是否有相同Unicode的字符串常量,如果有,则返回其的引用,如果没有,则在常量池中增加一个Unicode等于str的字符串并返回它的引用;看例3就清楚了

  例3:

String s0= “kvill”;
String s1=new String(”kvill”);
String s2=new String(“kvill”);
System.out.println( s0==s1 );
System.out.println( “**********” );
s1.intern();
s2=s2.intern(); //把常量池中“kvill”的引用赋给s2
System.out.println( s0==s1);
System.out.println( s0==s1.intern() );
System.out.println( s0==s2 );

  结果为:

false
**********
false //虽然执行了s1.intern(),但它的返回值没有赋给s1
true //说明s1.intern()返回的是常量池中”kvill”的引用
true

  最后我再破除一个错误的理解:

  有人说,“使用String.intern()方法则可以将一个String类的保存到一个全局String表中,如果具有相同值的Unicode字符串已经在这个表中,那么该方法返回表中已有字符串的地址,如果在表中没有相同值的字符串,则将自己的地址注册到表中“如果我把他说的这个全局的String表理解为常量池的话,他的最后一句话,“如果在表中没有相同值的字符串,则将自己的地址注册到表中”是错的:

  看例4:

String s1=new String("kvill");
String s2=s1.intern();
System.out.println( s1==s1.intern() );
System.out.println( s1+" "+s2 );
System.out.println( s2==s1.intern() );

  结果:

false
kvill kvill
true

  在这个类中我们没有声名一个”kvill”常量,所以常量池中一开始是没有”kvill”的,当我们调用s1.intern()后就在常量池中新添加了一个”kvill”常量,原来的不在常量池中的”kvill”仍然存在,也就不是“将自己的地址注册到常量池中”了。

  s1==s1.intern()为false说明原来的“kvill”仍然存在;

  s2现在为常量池中“kvill”的地址,所以有s2==s1.intern()为true。

  5. 关于equals()和==:

  这个对于String简单来说就是比较两字符串的Unicode序列是否相当,如果相等返回true;而==是比较两字符串的地址是否相同,也就是是否是同一个字符串的引用。

  6. 关于String是不可变的

  这一说又要说很多,大家只要知道String的实例一旦生成就不会再改变了,比如说:String str=”kv”+”ill”+” “+”ans”;
就是有4个字符串常量,首先”kv”和”ill”生成了”kvill”存在内存中,然后”kvill”又和” “ 生成 ”kvill “存在内存中,最后又和生成了”kvill ans”;并把这个字符串的地址赋给了str,就是因为String的“不可变”产生了很多临时变量,这也就是为什么建议用StringBuffer的原因了,因为StringBuffer是可改变的</pre>
<p> 出处:<a href="http://www.iteye.com/topic/122206">http://www.iteye.com/topic/122206</a></p>
<p> </p>
<p>By the way,关于 String.intern() 在实际中的应用,我在tomcat的源码中找到了一个地方用到了,如下:</p>
<p> </p>
<pre class="java" name="code">/*
* Copyright 1999,2004-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/

package org.apache.jasper.xmlparser;

/**
* This class is a symbol table implementation that guarantees that
* strings used as identifiers are unique references. Multiple calls
* to <code>addSymbol</code> will always return the same string
* reference.
* <p>
* The symbol table performs the same task as <code>String.intern()</code>
* with the following differences:
* <ul>
* <li>
* A new string object does not need to be created in order to
* retrieve a unique reference. Symbols can be added by using
* a series of characters in a character array.
* </li>
* <li>
* Users of the symbol table can provide their own symbol hashing
* implementation. For example, a simple string hashing algorithm
* may fail to produce a balanced set of hashcodes for symbols
* that are <em>mostly</em> unique. Strings with similar leading
* characters are especially prone to this poor hashing behavior.
* </li>
* </ul>
*
* @author Andy Clark
* @version $Id: SymbolTable.java 306179 2005-07-27 15:12:04Z yoavs $
*/
public class SymbolTable {

//
// Constants
//

/** Default table size. */
protected static final int TABLE_SIZE = 101;

//
// Data
//

/** Buckets. */
protected Entry[] fBuckets = null;

// actual table size
protected int fTableSize;

//
// Constructors
//

/** Constructs a symbol table with a default number of buckets. */
public SymbolTable() {
this(TABLE_SIZE);
}

/** Constructs a symbol table with a specified number of buckets. */
public SymbolTable(int tableSize) {
fTableSize = tableSize;
fBuckets = new Entry[fTableSize];
}

//
// Public methods
//

/**
* Adds the specified symbol to the symbol table and returns a
* reference to the unique symbol. If the symbol already exists,
* the previous symbol reference is returned instead, in order
* guarantee that symbol references remain unique.
*
* @param symbol The new symbol.
*/
public String addSymbol(String symbol) {

// search for identical symbol
int bucket = hash(symbol) % fTableSize;
int length = symbol.length();
OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
if (length == entry.characters.length) {
for (int i = 0; i < length; i++) {
if (symbol.charAt(i) != entry.characters[i]) {
continue OUTER;
}
}
return entry.symbol;
}
}

// create new entry
Entry entry = new Entry(symbol, fBuckets[bucket]);
fBuckets[bucket] = entry;
return entry.symbol;

} // addSymbol(String):String

/**
* Adds the specified symbol to the symbol table and returns a
* reference to the unique symbol. If the symbol already exists,
* the previous symbol reference is returned instead, in order
* guarantee that symbol references remain unique.
*
* @param buffer The buffer containing the new symbol.
* @param offset The offset into the buffer of the new symbol.
* @param length The length of the new symbol in the buffer.
*/
public String addSymbol(char[] buffer, int offset, int length) {

// search for identical symbol
int bucket = hash(buffer, offset, length) % fTableSize;
OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
if (length == entry.characters.length) {
for (int i = 0; i < length; i++) {
if (buffer[offset + i] != entry.characters[i]) {
continue OUTER;
}
}
return entry.symbol;
}
}

// add new entry
Entry entry = new Entry(buffer, offset, length, fBuckets[bucket]);
fBuckets[bucket] = entry;
return entry.symbol;

} // addSymbol(char[],int,int):String

/**
* Returns a hashcode value for the specified symbol. The value
* returned by this method must be identical to the value returned
* by the <code>hash(char[],int,int)</code> method when called
* with the character array that comprises the symbol string.
*
* @param symbol The symbol to hash.
*/
public int hash(String symbol) {

int code = 0;
int length = symbol.length();
for (int i = 0; i < length; i++) {
code = code * 37 + symbol.charAt(i);
}
return code & 0x7FFFFFF;

} // hash(String):int

/**
* Returns a hashcode value for the specified symbol information.
* The value returned by this method must be identical to the value
* returned by the <code>hash(String)</code> method when called
* with the string object created from the symbol information.
*
* @param buffer The character buffer containing the symbol.
* @param offset The offset into the character buffer of the start
* of the symbol.
* @param length The length of the symbol.
*/
public int hash(char[] buffer, int offset, int length) {

int code = 0;
for (int i = 0; i < length; i++) {
code = code * 37 + buffer[offset + i];
}
return code & 0x7FFFFFF;

} // hash(char[],int,int):int

/**
* Returns true if the symbol table already contains the specified
* symbol.
*
* @param symbol The symbol to look for.
*/
public boolean containsSymbol(String symbol) {

// search for identical symbol
int bucket = hash(symbol) % fTableSize;
int length = symbol.length();
OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
if (length == entry.characters.length) {
for (int i = 0; i < length; i++) {
if (symbol.charAt(i) != entry.characters[i]) {
continue OUTER;
}
}
return true;
}
}

return false;

} // containsSymbol(String):boolean

/**
* Returns true if the symbol table already contains the specified
* symbol.
*
* @param buffer The buffer containing the symbol to look for.
* @param offset The offset into the buffer.
* @param length The length of the symbol in the buffer.
*/
public boolean containsSymbol(char[] buffer, int offset, int length) {

// search for identical symbol
int bucket = hash(buffer, offset, length) % fTableSize;
OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
if (length == entry.characters.length) {
for (int i = 0; i < length; i++) {
if (buffer[offset + i] != entry.characters[i]) {
continue OUTER;
}
}
return true;
}
}

return false;

} // containsSymbol(char[],int,int):boolean

//
// Classes
//

/**
* This class is a symbol table entry. Each entry acts as a node
* in a linked list.
*/
protected static final class Entry {

//
// Data
//

/** Symbol. */
public String symbol;

/**
* Symbol characters. This information is duplicated here for
* comparison performance.
*/
public char[] characters;

/** The next entry. */
public Entry next;

//
// Constructors
//

/**
* Constructs a new entry from the specified symbol and next entry
* reference.
*/
public Entry(String symbol, Entry next) {
this.symbol = symbol.intern();
characters = new char[symbol.length()];
symbol.getChars(0, characters.length, characters, 0);
this.next = next;
}

/**
* Constructs a new entry from the specified symbol information and
* next entry reference.
*/
public Entry(char[] ch, int offset, int length, Entry next) {
characters = new char[length];
System.arraycopy(ch, offset, characters, 0, length);
symbol = new String(characters).intern();
this.next = next;
}

} // class Entry

} // class SymbolTable
</pre>
<p> </p>
</div>





<IFRAME SRC="/iframe_ggbd/187" SCROLLING=no WIDTH=468 HEIGHT=60 FRAMEBORDER=0></IFRAME>

<div id="bottoms" class="clearfix">

<div id="share_weibo">分享到:
<a data-type='sina' href="javascript:;" title="分享到新浪微博"><img src="/images/sina.jpg"></a>
<a data-type='qq' href="javascript:;" title="分享到腾讯微博"><img src="/images/tec.jpg"></a>
</div>
</div>

<div class="blog_nav">
<div class="pre_next">
<a href="/blog/549879" class="next" title="localhost map in windows">localhost map in windows</a>
|
<a href="/blog/547819" class="pre" title="动态加载 js">动态加载 js</a>
</div>
</div>
<div class="blog_bottom">
<ul>
<li>2009-12-16 11:33</li>
<li>浏览 7026</li>
<li><a href="#comments">评论(2)</a></li>


<li>分类:<a href="http://www.iteye.com/blogs/category/language">编程语言</a></li>
<li class='last'><a href="http://www.iteye.com/wiki/blog/549554" target="_blank" class="more">相关推荐</a></li>
</ul>
</div>

<div class="blog_comment">
<h5>评论</h5>
<a id="comments" name="comments"></a>
<div id="bc2373426">
<div class="comment_title">
2 楼
<a href='http://ffshi.iteye.com' target='_blank' title='ffshi'>ffshi</a>
2015-06-12  


</div>
<div class="comment_content"> 有人说,“使用String.intern()方法则可以将一个String类的保存到一个全局String表中,如果具有相同值的Unicode字符串已经在这个表中,那么该方法返回表中已有字符串的地址,如果在表中没有相同值的字符串,则将自己的地址注册到表中“如果我把他说的这个全局的String表理解为常量池的话,他的最后一句话,“如果在表中没有相同值的字符串,则将自己的地址注册到表中”是错的:   <br /><br /><br />如果在表中没有相同值的字符串,则将自己的地址注册到表中,应该是这么举例子吧:<br />这样才是true<br /><br />    String ss = new String("hello").intern();<br />    String s1 = "hello";<br />    <br />    System.out.println(ss == s1);</div>
</div>

<div id="bc2341469">
<div class="comment_title">
1 楼
<a href='http://vk14311.iteye.com' target='_blank' title='vk14311'>vk14311</a>
2014-03-06  


</div>
<div class="comment_content">String s1=new String("kvill");   <br />String s2=s1.intern();   <br />System.out.println( s1==s1.intern() );   <br />System.out.println( s1+" "+s2 );   <br />System.out.println( s2==s1.intern() ); <br /><br />这个例子应该改为:<br /><br />String s1=new String("kv") + "ill";   <br />String s2=s1.intern();   <br />System.out.println( s1==s1.intern() );   <br />System.out.println( s1+" "+s2 );   <br />System.out.println( s2==s1.intern() ); <br /><br />因为在String s1=new String("kvill"); 这句的时候已经存在"kvill"常量了。</div>
</div>




</div>

<div class="blog_comment">
<h5>发表评论</h5>
<p style="text-align:center; margin-top:30px;margin-bottom:0px;"><a href="/login" style="background-color:white;"> <img src="/images/login_icon.png" style="vertical-align:middle; margin-right: 10px;" /></a><a href="/login"> 您还没有登录,请您登录后再发表评论 </a></p>
</div>
</div>


<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code', true, true);

$$('#main .blog_content pre[name=code]').each(function(pre, index){ // blog content
var post_id = 549554;
var location = window.location;
source_url = location.protocol + "//" + location.host + location.pathname + location.search;
pre.writeAttribute('codeable_id', post_id);
pre.writeAttribute('codeable_type', "Blog");
pre.writeAttribute('source_url', source_url);
pre.writeAttribute('pre_index', index);
pre.writeAttribute('title', '理解String 及 String.intern() 在实际中的应用');
});

fix_image_size($$('div.blog_content img'), 700);

function processComment() {
$$('#main .blog_comment > div').each(function(comment){// comment
var post_id = comment.id.substr(2);
$$("#"+comment.id+" pre[name=code]").each(function(pre, index){
var location = window.location;
source_url = location.protocol + "//" + location.host + location.pathname + location.search;
source_url += "#" + comment.id;
pre.writeAttribute('codeable_id', post_id);
pre.writeAttribute('codeable_type', "BlogComment");
pre.writeAttribute('source_url', source_url);
pre.writeAttribute('pre_index', index);
pre.writeAttribute('title', '理解String 及 String.intern() 在实际中的应用');
});
});
}

function quote_comment(id) {
new Ajax.Request('/editor/quote', {
parameters: {'id':id, 'type':'BlogComment'},
onSuccess:function(response){editor.bbcode_editor.textarea.insertAfterSelection(response.responseText);
Element.scrollTo(editor.bbcode_editor.textarea.element);}
});
}

code_favorites_init();
processComment();
new WeiboShare({share_buttons: $('share_weibo'), img_scope: $('blog_content')});
</script>




</div>

<div id="local">
<div class="local_top"></div>
<div id="blog_owner">
<div id="blog_owner_logo"><a href='http://renxiangzyq.iteye.com'><img alt="lich0079的博客" class="logo" src="http://www.iteye.com/upload/logo/user/295686/25e9dc25-473b-3fcd-8a32-44ec8e0d993b.jpg?1282218216" title="lich0079的博客: 任香980101" width="" /></a></div>
<div id="blog_owner_name">lich0079</div>
</div>

<div id="blog_actions">
<ul>
<li>浏览: 577695 次</li>
<li>性别: <img alt="Icon_minigender_1" src="http://www.iteye.com/images/icon_minigender_1.gif?1324994303" title="男" /></li>
<li>来自: 上海</li>
<li><img src='/images/status/offline.gif'/></li>

</ul>
</div>
<div id="user_visits" class="clearfix">
<h5>最近访客 <span style='font-weight:normal;font-size:12px;padding-left:30px;'><a href="/blog/user_visits">更多访客>></a></span></h5>

<div class="user_visit">
<div class="logo"><a href='http://294676001.iteye.com' target='_blank'><img alt="294676001的博客" class="logo" src="http://www.iteye.com/upload/logo/user/231136/2bb14be7-efc7-3d5f-8e04-b44305a794c5-thumb.jpg?1270715760" title="294676001的博客: 梦的放肆" width="48px" /></a></div>
<div class="left"><a href='http://294676001.iteye.com' target='_blank' title='294676001'>294676001</a></div>
</div>

<div class="user_visit">
<div class="logo"><a href='http://feynman0127.iteye.com' target='_blank'><img alt="feynman0127的博客" class="logo" src="http://www.iteye.com/images/user-logo-thumb.gif?1324994303" title="feynman0127的博客: " width="48px" /></a></div>
<div class="left"><a href='http://feynman0127.iteye.com' target='_blank' title='feynman0127'>feynman0127</a></div>
</div>

<div class="user_visit">
<div class="logo"><a href='http://yjc2020.iteye.com' target='_blank'><img alt="yjc2020的博客" class="logo" src="http://www.iteye.com/images/user-logo-thumb.gif?1324994303" title="yjc2020的博客: yjc2020" width="48px" /></a></div>
<div class="left"><a href='http://yjc2020.iteye.com' target='_blank' title='yjc2020'>yjc2020</a></div>
</div>

<div class="user_visit">
<div class="logo"><a href='http://a997353546.iteye.com' target='_blank'><img alt="a997353546的博客" class="logo" src="http://www.iteye.com/images/user-logo-thumb.gif?1324994303" title="a997353546的博客: " width="48px" /></a></div>
<div class="left"><a href='http://a997353546.iteye.com' target='_blank' title='a997353546'>a997353546</a></div>
</div>

</div>



<div id="blog_menu">
<h5>文章分类</h5>
<ul>
<li><a href="/">全部博客 (454)</a></li>

<li><a href="/category/36285">设计模式 (11)</a></li>

<li><a href="/category/58092">学习资料 (5)</a></li>

<li><a href="/category/64394">笔记 (38)</a></li>

<li><a href="/category/71571">Extjs (39)</a></li>

<li><a href="/category/71751">数据库 (29)</a></li>

<li><a href="/category/72673">pushlet (5)</a></li>

<li><a href="/category/73507">js (61)</a></li>

<li><a href="/category/73508">Eclipse & RCP (15)</a></li>

<li><a href="/category/74339">mac (13)</a></li>

<li><a href="/category/82718">python (7)</a></li>

<li><a href="/category/84027">数据结构与算法 (11)</a></li>

<li><a href="/category/84521">linux (39)</a></li>

<li><a href="/category/84529">J2EE (75)</a></li>

<li><a href="/category/89330">struts (19)</a></li>

<li><a href="/category/89333">HIbernate (20)</a></li>

<li><a href="/category/89336">spring (14)</a></li>

<li><a href="/category/90363">life (7)</a></li>

<li><a href="/category/94696">Thread (8)</a></li>

<li><a href="/category/98954">test (5)</a></li>

<li><a href="/category/101342">css (6)</a></li>

<li><a href="/category/154411">ios (33)</a></li>

</ul>
</div>
<div id='month_blogs'>
<h5>社区版块</h5>
<ul>
<li><a href="/blog/news">我的资讯</a> (0)</li>
<li>
<a href="/blog/post">我的论坛</a> (124)
</li>
<li><a href="/blog/answered_problems">我的问答</a> (4)</li>
</ul>
</div>
<div id="month_blogs">
<h5>存档分类</h5>
<ul>

<li><a href="/blog/monthblog/2015-09">2015-09</a> (2)</li>

<li><a href="/blog/monthblog/2015-08">2015-08</a> (1)</li>

<li><a href="/blog/monthblog/2015-07">2015-07</a> (1)</li>

<li><a href="/blog/monthblog_more">更多存档...</a></li>
</ul>
</div>


<div id="comments_top">
<h5>评论排行榜</h5>
<ul>

<li><a href="/blog/2195479" title="编程最兴奋或难忘的时刻">编程最兴奋或难忘的时刻</a></li>

<li><a href="/blog/2234413" title="LockSupport 源码阅读">LockSupport 源码阅读</a></li>

<li><a href="/blog/2243957" title="tomcat 7 应用不能访问 及 配置管理界面 ">tomcat 7 应用不能访问 及 配置管理界面 ...</a></li>

<li><a href="/blog/2215836" title="iteye blog 备份">iteye blog 备份</a></li>

</ul>
</div>


<div id="guest_books">
<h5>最新评论</h5>
<ul>

<li>
<a href='http://string2020.iteye.com' target='_blank' title='string2020'>string2020</a>:
manager-script是干啥的?<br />
<a href="/blog/2243957#bc2378086">tomcat 7 应用不能访问 及 配置管理界面 </a>
</li>

<li>
<a href='http://quarterlifeforjava.iteye.com' target='_blank' title='QuarterLifeForJava'>QuarterLifeForJava</a>:
貌似不止一个,CyclicBarrier cb = new C ...<br />
<a href="/blog/2234413#bc2376442">LockSupport 源码阅读</a>
</li>

<li>
<a href='http://ffshi.iteye.com' target='_blank' title='ffshi'>ffshi</a>:
 有人说,“使用String.intern()方法则可以将一个 ...<br />
<a href="/blog/549554#bc2373426">理解String 及 String.intern() 在实际中的应用</a>
</li>

<li>
<a href='http://zhangchengtest.iteye.com' target='_blank' title='zhangchengtest'>zhangchengtest</a>:
mark <br />
<a href="/blog/2215836#bc2372846">iteye blog 备份</a>
</li>

<li>
<a href='http://232739206.iteye.com' target='_blank' title='岭岭睡不醒'>岭岭睡不醒</a>:
交活拿钱,最兴奋。。别的都是扯淡 <br />
<a href="/blog/2195479#bc2369316">编程最兴奋或难忘的时刻</a>
</li>

</ul>
</div>

<div class="local_bottom"></div>

</div>
</div>

<div id="footer" class="clearfix">
<div id="copyright">
<hr/>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。若作者同意转载,必须以超链接形式标明文章原始出处和作者。<br />
© 2003-2015 ITeye.com. All rights reserved. [ 京ICP证110151号 京公网安备110105010620 ]
</div>
</div>
</div>
<script type="text/javascript">
document.write("<img src='http://stat.iteye.com/?url="+ encodeURIComponent(document.location.href) + "&referrer=" + encodeURIComponent(document.referrer) + "&user_id=' width='0' height='0' />");
</script>

<script src="http://csdnimg.cn/pubfooter/js/tracking.js?version=20130923164150" type="text/javascript"></script>


<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7929382-6']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>


</body>
</html>
点击打开链接
在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字前面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字前面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值