java DataOutputStream和DataInputstream_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java DataOutputStream和DataInputstream

java DataOutputStream和DataInputstream

 2014/5/3 12:44:01  twobrushes  程序员俱乐部  我要评论(0)
  • 摘要:文章来源:http://www.itnose.net/detail/6034227.html更多文章:http://www.itnose.net/type/1.html应用实例importjava.io.*;publicclassDataStreamDemo{publicstaticvoidmain(String[]args){try{DataOutputStreamout=newDataOutputStream(newFileOutputStream("F
  • 标签:Java

文章来源:http://www.itnose.net/detail/6034227.html?更多文章:http://www.itnose.net/type/1.html

java,dataoutputstream,datainputstream0

应用实例

?

class="sycode" name="code">import java.io.*;

public class DataStreamDemo
{
	public static void main(String[] args)
	{
		try 
		{
			DataOutputStream out=new DataOutputStream(new FileOutputStream("F:\\workspace\\JavaPrj\\test.txt"));
			double[] prices={18.99,9.22,14.22,5.22,4.21};
			int[] units={10,10,20,39,40};
			String[] descs={"bike","book","boat","boot","bus"};
			for(int i=0;i<prices.length;i++)
			{
				try 
				{
					out.writeDouble(prices[i]);
					out.writeChar('\t');
					out.writeInt(units[i]);
					out.writeChar('\t');
					out.writeChars(descs[i]);
					out.writeChar('\n');
				} 
				catch (IOException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			try 
			{
				out.close();
			} 
			catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try 
		{
			DataInputStream in=new DataInputStream(new FileInputStream("F:\\workspace\\JavaPrj\\test.txt"));
			double prices;
			int num;
			StringBuffer name;
			char chr;
			int len;
			
			try 
			{
				while(true)
				{
					len=5;
					name=new StringBuffer();
					prices=in.readDouble();
					in.readChar();
					num=in.readInt();
					in.readChar();
					while((chr=in.readChar())!='\n')
					{
						name.append(chr);
					}
					//double类型转String类型以及添加空格对齐的方法
					System.out.println("产品名称:"+name+"\t价格:"+prices+getBlanks(len-Double.toString(prices).length())+"\t数量:"+num);
				} 
			}
			catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//添加空格进行对齐操作
	public static String getBlanks(int n)
	{
		String str="";
		while(n>0)
		{
			str+=" ";
			n--;
		}
		return str;
	}
}
java,dataoutputstream,datainputstream1
发表评论
用户名: 匿名