IO流缓冲区的读写方法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u013931330/article/details/41728043


缓冲区

缓冲区的出现是为了提高流的操作效率而出现的

所以在创建缓冲区前,必须要先有流对象

public static void main(String[]args)throws IOException

{

//创建一个字符写入流对象

FileWriter fw=new FileWriter("buftxt");

//为了提高字符写入流效率。加入了缓冲技术

//只要将需要被提高效率的流对象作为参数传递给缓冲区的构造函数即可

BufferedWriter bufw=new BufferedWriter(fw);

for(int i=0;i<5;i++)

{

butw.writer("abcd"+x);

butw.newLine(); //缓冲区的特有换行(能跨平台)

bufw.flush(); //用到缓冲区,就要刷新

}

bufw.close(); //关闭缓冲区,就是在关闭缓冲区中的流对象

}

public static void main(String[]args)throws IOException

{

//创建一个读取流对象和文件相关联。   

FileReader fr=new FileReader("buf.txt");

//为了提高效率。加入缓冲技术。将字符读取流对象作为参数传递给缓冲对象的构造函数

BufferedReader bufr=new BufferedReader(fr);

String line=null;

//当返回值为null时表示已读到末尾

while((line=bufr.readLine())!=null)   //readLine()   读一行数据

{

System.out.println(line);

}

bufr.close();

}

 

 


展开阅读全文

生产者消费者问题中,缓冲区的读写问题

11-28

linux下gcc;rn5个生产者线程,5个消费者线程,生产者从1开始写,一直写到500。rn用一个结构体数据模拟读写缓冲区,每写一次对其遍历排序一次,将未被读取的数据置前。rnrn问题:rn(消费线程中)rn当一个消费者线程持续获取缓冲区锁 并占有时,出现读取数据失败的情况。rn(ps:消费线程切换则不会出现这种情况;rn可能描述比较模糊,运行一下就能看到现象了,[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/83.gif][/img]!)rnrn#include "common.h"rnrn//common中含头文件和 模拟缓冲区的结构体s_clientrn/*rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rnrntypedef struct s_clientrnrn int clientid;//数值rn int status;//标示是否有值,有值=1,无值=0rn;rn*/rnrn//main.crn#define MAX_CONSUMERS 5 //消费线程数rn#define MAX_PRODUCERS 5 //生产线程数rn#define MAX_BUFFERS 20 //缓冲区可存数据rn#define MAX_NUMBER 500 //生产总数rnrnFILE *fp, *fp_out, *fp_in;//可无视,in.txt 与 123.txt 结合stdout 可看出问题点buff内数据>2rnrnpthread_mutex_t full_mutex,empty_mutex,file_mutex;rn//分别为消费、生产、缓冲区互斥锁rnrnvolatile int buff_counts = 0, exit_flag = 0, i_in = 0, exit_flag_consumer = 0;rn//exit_flag、exit_flag_consumer分别为生产、消费线程退出条件,i_in 取值 1至500rnrnvolatile struct s_client buff[MAX_BUFFERS]; //读写缓冲区rnrn//生产者写缓冲区rnint write_one(volatile struct s_client *buffer, int num)rnrn int i,j,sum=0;rnrn//遍历,将未被读取的数据置前rn for(i=0;iMAX_NUMBER || exit_flag)rn rn exit_flag = 1;rn break;rn rnrn pthread_mutex_lock(&file_mutex);rn fprintf(fp,"%d\n", i_in);rn //fflush(fp);rn write_one(buff, i_in);rn buff_counts++;rn pthread_mutex_unlock(&file_mutex);rn rn //printf("pid=%04d producet %d \n", pthread_self()%10000, i_in);rn fprintf(fp_out, "pid=%04d producet %d \n", pthread_self()%10000, i_in);rn usleep(1000);rn rn pthread_mutex_unlock(&empty_mutex);rn //usleep(10000);rn if(i_in>MAX_NUMBER || exit_flag) break;rn rn printf("producer %04d exited!\n", pthread_self()%10000);rn fprintf(fp_out, "producer %04d exited!\n", pthread_self()%10000);rn pthread_exit("produce over!");rnrnrn//消费者线程rnint consumer()rnrn int i;rn while(1)rn rn pthread_mutex_lock(&full_mutex);rn rn while(buff_counts>0)rn rn if(exit_flag_consumer) break;rn rn pthread_mutex_lock(&file_mutex);rn i = get_one(buff);rn if(i<0)rn rn printf("get_one failed!\n");rn pthread_mutex_unlock(&file_mutex);rn continue;rn rn fprintf(fp, "buff_counts = %d //pid=%04d consume %d\n", buff_counts, pthread_self()%10000,i);rn if(i==MAX_NUMBER)rn rn exit_flag_consumer = 1;rn rn printf("pid=%04d consume %d\n", pthread_self()%10000,i);rn buff_counts--;rn pthread_mutex_unlock(&file_mutex); rn rn if(exit_flag_consumer)rn rn exit_flag = 1;rn break;rn rn //usleep(1000);rn//this sentence will make the get_one(buff) failed in the old threadrn//问题出现在这里,如果这里用usleep,这一个消费者线程继续读取的缓冲区buff会失败(当缓冲区多余2个以上数据时)rn//若不使用usleep,其他消费者线程获取缓冲区锁,不会出现读取失败的问题。rn rn pthread_mutex_unlock(&full_mutex);rn if(i==MAX_NUMBER || exit_flag) break;rn usleep(1000);rn rn printf("consumer %04d exited!\n", pthread_self()%10000);rn pthread_exit("consume over!");rnrnrnint main(int argc, int argv[])rnrn pthread_t consumer_queue[MAX_CONSUMERS], producer_queue[MAX_PRODUCERS];rn int i,result;rn char temp[10];rn void *message;rnrn memset((void *)buff,0,sizeof(buff));rn fp = fopen("./123.txt", "wr+");rn fp_in = fopen("./in.txt", "w+");rn fp_out = fopen("./out.txt", "w+r");rn if(!fp)rn rn perror("fopen failed!");rn exit(1);rn rn result = pthread_mutex_init(&full_mutex, NULL);rn if(result)rn rn perror("pthread_mutex_init failed!");rn exit(1);rn rn rn result = pthread_mutex_init(&empty_mutex, NULL);rn if(result)rn rn perror("pthread_mutex_init failed!");rn exit(1);rn rn rn result = pthread_mutex_init(&file_mutex, NULL);rn if(result)rn rn perror("pthread_mutex_init failed!");rn exit(1);rn rn rn//消费线程rn for(i = 0; i=0; i--)rn rn pthread_join(producer_queue[i], &message);rn printf("return message is: %s\n", (char*)message);rn rn sleep(1);rn fclose(fp);rn exit(0);rn rnrn 论坛

没有更多推荐了,返回首页