package javaapplication11;
import java.util.Scanner;
import java.io.*;
/**
*
* @author jenison-3631
*/
public class JavaApplication11 {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException
{
// TODO code application logic here
// File file = new File("/Users/jenison-3631/Desktop/csvv.txt");
int n,z=1;
FileWriter writr = new FileWriter("/Users/jenison-3631/Desktop/csvv.txt");
FileReader fr= new FileReader("/Users/jenison-3631/Desktop/csvv.txt");
BufferedReader br=new BufferedReader(fr);
BufferedWriter bw= new BufferedWriter(writr);
try{
while(z==1)
{
System.out.println("please enter your choice\n1.Add number\n2.Delete number\n3.List all\n4.Search number");
Scanner s= new Scanner(System.in);
n= s.nextInt();
switch(n)
{
case 1:
String str;
String number;
System.out.println("Enter the name");
s.nextLine();
str= s.nextLine();
System.out.println("Enter the number");
number=s.nextLine();
System.out.println(str+" "+number);
/* writer.append(str);
writer.append(',');
writer.append(number);
writer.append('\n');*/
String actual=str+","+number+"\n";
bw.write(actual,0,actual.length());
break;
case 2:
String del=null;
String line=null;
String spl=",";
System.out.println("Enter the name whose phone number should be deleted");
s.nextLine();
del=s.nextLine();
while((line=br.readLine())!=null)
{
String[] country = line.split(spl);
System.out.println("hai"+country[0]);
}
System.out.println(del);
break;
}
System.out.println("Do u wish to continue....if yes press 1 else press 2");
z= s.nextInt();
}
}
finally{
bw.close();
br.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的情况2当我尝试从文件csvv.txt中恢复名称时,它无法正常工作,因为该文件实际上没有数据.但是当我单独运行案例1时,数据在文件中是写的
BufferedWriter不会立即写入写入,因为它会缓冲给定的数据.
想象一下,你在贵公司的邮件室工作.有人带着信寄到客户端.你需要10分钟才能到街上的邮局寄邮件.你是否立刻接受了这封信,还是等着看其他人是否会先给你写信?
这是缓冲:而不是立即进行昂贵的操作(在街上行走),你可以等待并立即收集很多事情 - 如果你有100封信给邮件,你只需要10分钟就可以走下去这条街,即使你把它们放在邮箱里也要花费更长的时间.
它与计算机上的IO相同:它很昂贵.写入磁盘,发送到网络等是很慢的,所以如果你不需要,你不想重复这些.
但你是否应该关心缓冲正在发生?很大,没有.在邮件室的例子中,人们只想放下他们的信件,并知道它将在未来的某个时刻送达.一旦他们放弃它,无论你现在在街上奔跑还是先等待100个字母,对他们来说无关紧要.
在代码中,您通常不关心何时写入数据.它只是在某些时候写入,例如,当文件编写器关闭时,或者一旦你要求将一定数量的数据写入文件.
如果您在发生这些事情之前关心正在写入的数据,您可以调用bw.flush()以强制它立即发生.
您可以在Oracle的Essential Java教程中阅读有关IO和缓冲的更多信息.你可以从这里开始,但关于缓冲的一点就在这里
| 归档时间: |
|
| 查看次数: |
177 次 |
| 最近记录: |