MongoInit.java是数据库初始化及连接类

MongoUtils.java是对mongodb的各种操作方法

MongoInit.java

package com.wlwcloud.datatest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Properties; import org.apache.log4j.Logger; import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress; /**
* Mongo工具类:设计为单例模式,每当月份发生变化,数据库连接名称就会发生变化,这是业务规则 因MongoDB的Java驱动是线程安全的,对于一般的应用,仅仅要一个Mongo实例就可以,Mongo有个内置的连接池(池大小默觉得10个)。
* 对于有大量写和读的环境中,为了确保在一个Session中使用同一个DB时,我们能够用下面方式保证一致性: DB mdb =mongo.getDB('dbname'); mdb.requestStart(); // 业务代码 mdb.requestDone();
* DB和DBCollection是绝对线程安全的
* @author undoner
*/
public class MongoInit {
/** DB监听端口号 */
private static int DBPort = 27017;
/** DB连接URL */
private static String DBUrl = "localhost";
/** 连接DB库名称 */
private static String DBName = "wlwdb";
static Logger log = Logger.getRootLogger();
private static Mongo mongo;
private static DBCollection coll;
private static DB db; static {
try {
DBproperties();
MongoOptions options = new MongoOptions();
options.autoConnectRetry = true;
options.connectionsPerHost = 1000;
options.maxWaitTime = 5000;
options.socketTimeout = 0;
options.connectTimeout = 15000;
options.threadsAllowedToBlockForConnectionMultiplier = 5000;
// 其实,Mongo实例代表了一个数据库连接池,即使在多线程的环境中,一个Mongo实例对我们来说已经足够了
ServerAddress serverAddress = new ServerAddress(DBUrl, DBPort);
mongo = new Mongo(serverAddress, options);
} catch (UnknownHostException e) {
log.info("get mongo instance failed");
}
} static void DBproperties() {
// 读取配置文件config.properties中的属性值
String myFilePath = MongoInit.class.getResource("/").getPath() + "MongoDBConfig.properties";
Properties properties = new Properties();
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(myFilePath);
properties.load(fileInputStream);
DBPort = Integer.parseInt((String) properties.getProperty("DBPort"));
DBUrl = (String) properties.getProperty("DBUrl");
DBName = (String) properties.getProperty("DBName");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } public static DB getDB() {
if (db == null) {
db = mongo.getDB(DBName);
}
return db;
} public static Mongo getMong() {
return mongo;
} public static DBCollection getColl(String collname) {
return getDB().getCollection(collname);
} }

MongoUtils.java

package com.wlwcloud.datatest;

import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ketayao.ketacustom.entity.main.User;
import com.ketayao.utils.SecurityUtils;
import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.QueryBuilder;
import com.mongodb.WriteConcern; public class MongoUtils {
//DBCursor cursor = coll.find(condition).addOption(Bytes.QUERYOPTION_NOTIMEOUT);//设置游标不要超时 /**
* 获取全部数据库实例
*/
public void testGetDBS() {
List<String> dbnames = MongoInit.getMong().getDatabaseNames();
for (String dbname : dbnames) {
System.out.println("dbname:" + dbname);
}
} /**
* 删除数据库
*/
public void dropDatabase(String dbname) {
MongoInit.getMong().dropDatabase(dbname);
} /**
* 查询全部表名
*/
public void getAllCollections() {
Set<String> colls = MongoInit.getDB().getCollectionNames();
for (String s : colls) {
System.out.println("Collections:"+s);
}
} /**
* 删除一个表
*/
public void dropCollection(String collection) {
MongoInit.getColl(collection).drop();
} /**
* 加入一条记录
*/ public void addData(String dbCollection,String dataID,String dataTime,String dataContent,String dataStatus) {
DBCollection coll = MongoInit.getColl(dbCollection);
BasicDBObject doc = new BasicDBObject();
doc.put("id", dataID);
doc.put("time", dataTime);
doc.put("data", dataContent);
doc.put("status", dataStatus);
coll.insert(doc);
// 设定write concern,以便操作失败时得到提示
coll.setWriteConcern(WriteConcern.SAFE);
findOne(dbCollection);
} /**
* 创建索引
*/
public void createIndex(String collection) {
MongoInit.getColl(collection).createIndex(new BasicDBObject("index_id", 1));
} /**
* 获取索引信息
*/
public void getIndexInfo(String dbCollection) {
List<DBObject> list = MongoInit.getColl(dbCollection).getIndexInfo();
for (DBObject o : list) {
System.out.println(o);
}
} /**
* 加入多条记录
*/
public void addMultiData() {
for (int i = 0; i < 100; i++) {
MongoInit.getColl("wujintao").insert(
new BasicDBObject().append("i", i));
} List<DBObject> docs = new ArrayList<DBObject>();
for (int i = 0; i < 50; i++) {
docs.add(new BasicDBObject().append("i", i));
}
MongoInit.getColl("wujintao").insert(docs);
// 设定write concern,以便操作失败时得到提示
MongoInit.getColl("wujintao").setWriteConcern(WriteConcern.SAFE);
} /**
* 查找第一条记录
*/
public void findOne(String dbCollection) {
DBObject myDoc = MongoInit.getColl(dbCollection).findOne();
System.out.println(myDoc);
} /**
* 获取表中全部记录条数
*/
public void count(String dbCollection) {
System.out.println(MongoInit.getColl(dbCollection).getCount());
System.out.println(MongoInit.getColl(dbCollection).count());
} /**
* 获取查询结果集的记录数
*/
public void getCount(String dbCollection,String dataID) {
DBObject query = new BasicDBObject("id", dataID);
long count = MongoInit.getColl(dbCollection).count(query);
System.out.println(count);
} /**
* 查询全部结果
*/
public void getAllDocuments(String dbCollection) {
DBCursor cursor = MongoInit.getColl(dbCollection).find();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
} /**
* 依照一个条件查询
*/
public void queryByConditionOne() {
BasicDBObject query = new BasicDBObject();
query.put("name", "MongoDB");
DBCursor cursor = MongoInit.getColl("wujintao").find(query); try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
}
public List<SensorData> queryById(String id) {
User user = SecurityUtils.getLoginUser();
BasicDBObject query = new BasicDBObject();
System.out.print(id);
query.put("id", id);
String table="table_";
DBCursor cursor = MongoInit.getColl("table_"+user.getUsername()).find(query);
List<DBObject> list=cursor.toArray();
System.out.println(list.size());
System.out.println("dao.."+list.toString());
List<SensorData> sensordata=new ArrayList<SensorData>();
for(int i=1;i<list.size();i++)
{
String sid=(String)list.get(i).get("id");
String stime=(String)list.get(i).get("time");
String sdata=(String)list.get(i).get("data");
String status=(String)list.get(i).get("status");
String sstatus;
if(status.equals("0"))sstatus="正常";
else if(status.equals("1")) sstatus="删除";
else sstatus="未知状态";
SensorData a=new SensorData(sid,stime,sdata,sstatus);
//System.out.print(a.getData()+a.getId()+a.getStatus()+a.getTime());
sensordata.add(a);
}
return sensordata;
/* try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
} */
} /**
* AND多条件查询,区间查询
*/
public void queryMulti() {
BasicDBObject query = new BasicDBObject();
// 查询j不等于3,k大于10的结果集
query.put("j", new BasicDBObject("$ne", 3));
query.put("k", new BasicDBObject("$gt", 10));
DBCursor cursor = MongoInit.getColl("wujintao").find(query);
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
} /**
* 区间查询
* select * from table where i >50
*/
public void queryMulti2() {
BasicDBObject query = new BasicDBObject();
query = new BasicDBObject();
query.put("i", new BasicDBObject("$gt", 50)); // e.g. find all where i >
DBCursor cursor = MongoInit.getColl("wujintao").find(query);
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
} /**
* 区间查询
* select * from table where 20 < i <= 30
//比較符
//"$gt": 大于
//"$gte":大于等于
//"$lt": 小于
//"$lte":小于等于
//"$in": 包括
*/
public void queryMulti3() {
BasicDBObject query = new BasicDBObject();
query = new BasicDBObject(); query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30));
DBCursor cursor = MongoInit.getColl("wujintao").find(query);
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
} /**
* 组合in和and select * from test_Table where (a=5 or b=6) and (c=5 or d = 6)
*/
public void queryMulti4() {
BasicDBObject query11 = new BasicDBObject();
query11.put("a", 1);
BasicDBObject query12 = new BasicDBObject();
query12.put("b", 2);
List<BasicDBObject> orQueryList1 = new ArrayList<BasicDBObject>();
orQueryList1.add(query11);
orQueryList1.add(query12);
BasicDBObject orQuery1 = new BasicDBObject("$or", orQueryList1); BasicDBObject query21 = new BasicDBObject();
query21.put("c", 5);
BasicDBObject query22 = new BasicDBObject();
query22.put("d", 6);
List<BasicDBObject> orQueryList2 = new ArrayList<BasicDBObject>();
orQueryList2.add(query21);
orQueryList2.add(query22);
BasicDBObject orQuery2 = new BasicDBObject("$or", orQueryList2); List<BasicDBObject> orQueryCombinationList = new ArrayList<BasicDBObject>();
orQueryCombinationList.add(orQuery1);
orQueryCombinationList.add(orQuery2); BasicDBObject finalQuery = new BasicDBObject("$and",
orQueryCombinationList);
DBCursor cursor = MongoInit.getColl("wujintao").find(finalQuery);
} /**
* IN查询
* if i need to query name in (a,b); just use { name : { $in : ['a', 'b'] } }
* select * from things where name='a' or name='b'
* @param coll
*/
public void queryIn() {
BasicDBList values = new BasicDBList();
values.add("a");
values.add("b");
BasicDBObject in = new BasicDBObject("$in", values);
DBCursor cursor = MongoInit.getColl("wujintao").find(
new BasicDBObject("name", in));
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
} /**
* 或查询
* select * from table where name = '12' or title = 'p'
* @param coll
*/
public void queryOr() {
QueryBuilder query = new QueryBuilder();
query.or(new BasicDBObject("name", 12), new BasicDBObject("title", "p"));
DBCursor cursor = MongoInit.getColl("wujintao").find(query.get()).addSpecial("$returnKey", "");
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
} public void customQueryField() throws UnknownHostException{
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("zhongsou_ad");
BasicDBObjectBuilder bulder = new BasicDBObjectBuilder();
bulder.add("times",1);
bulder.add("aid",1);
DBCursor cusor = db.getCollection("ad_union_ad_c_1").find(new BasicDBObject(),bulder.get());
for (DBObject dbObject : cusor) {
System.out.println(dbObject);
}
} public void mapReduce() throws UnknownHostException{
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("zhongsou_ad");
/***
* book1 = {name : "Understanding JAVA", pages : 100}
* book2 = {name : "Understanding JSON", pages : 200}
* db.books.save(book1)
* db.books.save(book2)
* book = {name : "Understanding XML", pages : 300}
* db.books.save(book)
* book = {name : "Understanding Web Services", pages : 400}
* db.books.save(book)
* book = {name : "Understanding Axis2", pages : 150}
* db.books.save(book)
*
var map = function() {
var category;
if ( this.pages >= 250 )
category = 'Big Books';
else
category = "Small Books";
emit(category, {name: this.name});
};
var reduce = function(key, values) {
var sum = 0;
values.forEach(function(doc) {
sum += 1;
});
return {books: sum};
};
var count = db.books.mapReduce(map, reduce, {out: "book_results"});
*/
try { DBCollection books = db.getCollection("books"); BasicDBObject book = new BasicDBObject();
book.put("name", "Understanding JAVA");
book.put("pages", 100);
books.insert(book); book = new BasicDBObject();
book.put("name", "Understanding JSON");
book.put("pages", 200);
books.insert(book); book = new BasicDBObject();
book.put("name", "Understanding XML");
book.put("pages", 300);
books.insert(book); book = new BasicDBObject();
book.put("name", "Understanding Web Services");
book.put("pages", 400);
books.insert(book); book = new BasicDBObject();
book.put("name", "Understanding Axis2");
book.put("pages", 150);
books.insert(book); String map = "function() { "+
"var category; " +
"if ( this.pages >= 250 ) "+
"category = 'Big Books'; " +
"else " +
"category = 'Small Books'; "+
"emit(category, {name: this.name});}"; String reduce = "function(key, values) { " +
"var sum = 0; " +
"values.forEach(function(doc) { " +
"sum += 1; "+
"}); " +
"return {books: sum};} "; MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,
null, MapReduceCommand.OutputType.INLINE, null); MapReduceOutput out = books.mapReduce(cmd); for (DBObject o : out.results()) {
System.out.println(o.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
} public void GroupByManyField() throws UnknownHostException{
//此方法没有执行成功
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("libary");
DBCollection books = db.getCollection("books");
BasicDBObject groupKeys = new BasicDBObject();
groupKeys.put("total", new BasicDBObject("$sum","pages")); BasicDBObject condition = new BasicDBObject();
condition.append("pages", new BasicDBObject().put("$gt", 0)); String reduce = "function(key, values) { " +
"var sum = 0; " +
"values.forEach(function(doc) { " +
"sum += 1; "+
"}); " +
"return {books: sum};} ";
/**
BasicDBList basicDBList = (BasicDBList)db.getCollection("mongodb中集合编码或者编码")
.group(DBObject key, --分组字段,即group by的字段
DBObject cond, --查询中where条件
DBObject initial, --初始化各字段的值
String reduce, --每一个分组都须要执行的Function
String finial --终结Funciton对结果进行终于的处理
*/
DBObject obj = books.group(groupKeys, condition, new BasicDBObject(), reduce);
System.out.println(obj); AggregationOutput ouput = books.aggregate(new BasicDBObject("$group",groupKeys));
System.out.println(ouput.getCommandResult());
System.out.println(books.find(new BasicDBObject("$group",groupKeys)));
} /**
* 分页查询
*/
public void pageQuery() {
DBCursor cursor = MongoInit.getColl("wujintao").find().skip(0)
.limit(10);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} /**
* 模糊查询
*/
public void likeQuery() {
Pattern john = Pattern.compile("joh?n");
BasicDBObject query = new BasicDBObject("name", john); // finds all people with "name" matching /joh?n/i
DBCursor cursor = MongoInit.getColl("wujintao").find(query);
} /**
* 条件删除
*/
public void delete(String dbCollection,String dataID) {
BasicDBObject query = new BasicDBObject();
query.put("id", dataID);
// 找到而且删除,并返回删除的对象
DBObject removeObj = MongoInit.getColl(dbCollection).findAndRemove(query);
System.out.println(removeObj);
} /**
* 更新
*/
public void update() {
BasicDBObject query = new BasicDBObject();
query.put("name", "liu");
DBObject stuFound = MongoInit.getColl("wujintao").findOne(query);
stuFound.put("name", stuFound.get("name") + "update_1");
MongoInit.getColl("wujintao").update(query, stuFound);
} public void testGpsData() {
try {
//13862082455:117.13172:029.77659:131.2580
Date d = new Date();
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String dateNowStr = sdf.format(d);
System.out.println(dateNowStr); DBCollection coll = MongoInit.getColl("table_admin");
BasicDBObject doc = new BasicDBObject();
doc.put("id", "12345678");
doc.put("time", dateNowStr);
doc.put("data", "117.13172:029.77659:131.2580");
doc.put("status", "0");
coll.insert(doc);
// 设定write concern,以便操作失败时得到提示
coll.setWriteConcern(WriteConcern.SAFE); BasicDBObject dsort = new BasicDBObject();
dsort.put("time", -1); BasicDBObject searchDevice = new BasicDBObject();
searchDevice.put("id", "12345678");
DBCursor cursor = coll.find(searchDevice).sort(dsort); List<DBObject> list = cursor.toArray();
//ArrayList<SensorData> listSensorData=new ArrayList<SensorData>(list.size()); ObjectMapper mapper = new ObjectMapper();
String jsonfromList = null;
try {
//list转json
jsonfromList = mapper.writeValueAsString(list);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(jsonfromList);
// for(int i=0;i<list.size();i++)
// {
// System.out.println((String) list.get(i).get("time"));
//} //System.out.println(list.size()+","+listSensorData);//list的长度
// System.out.println(cursor.count());//计算结果的数量,相似于(mysql count()函数),不受limit的影响 // while (cursor.hasNext()) {
// System.out.println(cursor.next());
// }
System.out.println("The Search Query has Executed!");
} catch (MongoException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
String dbname="wlwdb";
String dbCollection="table_admin";
String dbCollection1="15257102317";
String dataID="12345678";
MongoUtils tb=new MongoUtils();
tb.testGetDBS();//查询全部数据库
tb.getAllCollections();//查询全部表
//
// tb.findOne(dbCollection);//查找第一条记录
tb.testGpsData();
tb.getAllDocuments(dbCollection);//获取制定表明的全部数据
//tb.dropDatabase(dbname);//删除指定数据库
//tb.delete(dbCollection,dataID);//删除制定表明和设备ID的数据
//tb.dropCollection(dbCollection);// 删除一个表 //tb.createIndex(dbCollection1);//创建索引
//tb.getIndexInfo(dbCollection1);//获得表中全部索引
}
}

MongoDBConfig.properties

#端口设置
DBPort=27017
#URL
DBUrl=localhost
#URL
DBName=wlwdb

MongoDB之Java測试代码(DAO层)的更多相关文章

  1. Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完毕測试代码)

    MD5和RSA是网络传输中最经常使用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,仅仅能加密而不能解密. ...

  2. maven多module项目中千万不要引入其它模块的单元測试代码

    本文出处:http://blog.csdn.net/chaijunkun/article/details/35796335,转载请注明. 因为本人不定期会整理相关博文,会对对应内容作出完好. 因此强烈 ...

  3. 基于redis集群实现的分布式锁,可用于秒杀商品的库存数量管理,有測试代码(何志雄)

    转载请标明出处. 在分布式系统中,常常会出现须要竞争同一资源的情况,本代码基于redis3.0.1+jedis2.7.1实现了分布式锁. redis集群的搭建,请见我的另外一篇文章:<>& ...

  4. Java測试覆盖率工具----Cobertura,EclEmma

    Cobertura 是一个与Junit集成的代码覆盖率測量工具 它是免费.开源的 它能够与Ant和Maven集成.也能够通过命令行调用 能够生成HTML或XML格式的报告 能够依照不同的标准对HTML ...

  5. OpenFace库(Tadas Baltrusaitis)中基于Haar Cascade Classifiers进行人脸检測的測试代码

    Tadas Baltrusaitis的OpenFace是一个开源的面部行为分析工具.它的源代码能够从 https://github.com/TadasBaltrusaitis/OpenFace 下载. ...

  6. DIV旋转的測试代码

    <html> <head> <style type="text/css"> .rat0 { -webkit-transform: rotate( ...

  7. Tensorflow MNIST 数据集測试代码入门

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 測试代码已上传至GitH ...

  8. Java通过JDBC 进行Dao层的封装

    前言 前面有一章节,我专门讲解了Java通过JDBC 进行MySQL数据库操作,这主要讲解了MySQL数据库的连接和简单的操作,但是在真正的Java项目中,我们要不断的和数据库打交道,为了提高数据库操 ...

  9. Testng 的数据源 驱动測试 代码与配置

    JUnit中有讲述使用注解的方式进行数据源读取进行自己主动循环測试的方法,在TestNG中也提供了对应的方法 public class TestngDataProvider { /** * 数组内的每 ...

随机推荐

  1. Android Camera解析(上) 调用系统相机拍摄照片

    开发中我们常须要通过相机获取照片(拍照上传等).一般通过调用系统提供的相机应用就可以满足需求:有一些复杂需求还须要我们自己定义相机相关属性,下篇我们会涉及到. 首先我们来研究怎样简单调用系统相机应用来 ...

  2. Chromium网页输入事件捕捉和手势检測过程分析

    连续的输入事件可能会产生一定的手势操作.比如滑动手势和捏合手势. 在Chromium中,网页的输入事件是在Browser进程中捕捉的.Browser进程捕获输入事件之后,会进行手势操作检測.检測出来的 ...

  3. 《C++编程思想》第四章 初始化与清除(原书代码+习题+解答)

    相关代码: 1. #include <stdio.h> class tree { int height; public: tree(int initialHeight); ~tree(); ...

  4. Codeforces Round #272 (Div. 2) 题解

    Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...

  5. HDU 5654 xiaoxin and his watermelon candy 离线树状数组

    xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...

  6. 非Qt工程使用Qt的信号槽机制

    非Qt工程,使用Qt的信号槽机制,蛋疼不?反正我现在就是要做这样一件蛋疼的事. 要使用Qt的信号槽机制,下面是从Qt Assist里面关于 signal & slots 的一句介绍: All ...

  7. Struts2 的工作原理

    Struts2 的工作原理: 1)client向server发出一个http请求.webserver对请求进行解析,假设在StrutsPrepareAndExecuteFilter的请求映射路径(在w ...

  8. 0x22 迭代加深

    poj2248 真是个新套路.还有套路剪枝...大到小和判重 #include<cstdio> #include<iostream> #include<cstring&g ...

  9. js如何计算浮点数

    js中浮点型是如何运算的呢? 例如:var a=0.69; 我想得到6.9 直接这样写 var c=a*10; alert(c);   得到结果是:6.8999999999999995 到网上一搜,有 ...

  10. 5.文件I/O

    1 C标准函数与系统函数的区别 文件的结构体: 1.1 I/O缓冲区 每一个FILE文件流都有一个缓冲区buffer,默认大小8192Byte. 1.2 效率 文件缓冲区会降低效率.这里提供缓冲区主要 ...