zoukankan      html  css  js  c++  java
  • java 输入输出类

    java中输入输出类如果理不清思路的话就会很混乱!

      一.File类提供与操作系统无关的文件操作!可以查看api文档了解它的方法

      二.输出操作:

      1.把信息输出到屏幕上

      最简单的操作就是使用标准输出:System.out.println();

      下面讨论一下流方式的输出,与之相关的类如下

      OutputStreamWriter:它提供字符流到字节流的转换,换句话说它写入字符数据然后按照指定字符集转换为字节数据

      BufferedWriter:它可以将文本写入到字符流中,并且使用字符缓冲来提高写入的效率.建立在屏幕上输出字符的输出流应该采用如下代码:BufferedWriter bw=new BufferedWriter(new OutputStreamWriter());可以查看文档了解更多的方法.

      例子:import java.io.*;

      public class Hello{

      public static void main(String args[]){

      try{

      String filename="/home/wuxiaoxiao/1.txt";

      File file=new File(filename);

      if(file.exists())

      {

      System.out.println("这是基本的输出!");

      }else{

      BufferedWriter writerstream=new BufferedWriter(new OutputStreamWriter(System.out));

      String str="这是个特殊输出!";

      writerstream.write(str,0,str.length());

      writerstream.flush();

      writerstream.close();

      }

      }catch(IOException e){

      System.out.println(e);

      }

      }

      }

      2.向文件中输出字符数据:

      FileWriter:按照字符形式写入文件

      例子:import java.io.*;

      public class hello{

      public static void main(String args[]){

      try{

      BufferedWriter bw=new BufferedWriter(new FileWriter("/home/wuxiaoxiao/1.txt"));

      bw.write("hello java world!");

      bw.newLine();

      bw.write("你好啊!");

      bw.close();

      }catch(IOException e){

      System.out.println(e);

      }

      }

      }

      FileOutputStream按照字节形式写入数据

      例子:import java.io.*;

      public class hello{

      public static void main(String args[]){

      try{

      int v1=10;

      double v2=20.345D;

      String str="hello word!";

      DataOutputStream bw=new DataOutputStream(new FileOutputStream("/home/wuxiaoxiao/1.txt"));

      //向文件中写入二进制数据

      bw.writeInt(v1);

      bw.writeChars(str);

      bw.close();

      }catch(IOException e){

      System.out.println(e);

      }

      }

      打开文件会看到乱码,因为这是二进制形式!

      三.输入操作

      1.从键盘上读取字符数据

      InputStreamReader:它提供了字节流到字符流的转换

      BufferedReader:从字符流中读取文本,并且使用字符缓存来提高读取效率

      例子:import java.io.*;

      public class Hello{

      public static void main(String args[]){

      try{

      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

      System.out.println("输入:");

      String str=br.readLine();

      System.out.println("输入的数据是:"+str);

      }catch(IOException e){

      System.out.println(e);

      }

      }

      2.从文件中读取字符数据:

      FileReader:按照字符方式读取文件内容

      import java.io.*;

      public class Hello{

      public static void main(String args[]){

      try{

      BufferedReader br=new BufferedReader(new FileReader("/home/wuxiaoxiao/1.txt"));

      String in;

      while((in=br.readLine())!=null)

      System.out.println(in);

      }catch(IOException e){

      System.out.println(e);

      }

      }

      }

      3.从文件中读取字节数据

      FileInputStream:按照字节方式读取文件内容

      例子:import java.io.*;

      public class Hello{

      public static void main(String args[]){

      try{

      DataInputStream br=new DataInputStream(new FileInputStream("/home/wuxiaoxiao/1.txt"));

      //读取数据

      int v1=br.readInt();

      double v2=br.readDouble();

      byte[] buff=new byte[17];

      br.read(buff);

      String v3=new String(buff);

      //显示数据

      System.out.println(v1.....);

      }catch(IOException e){

      System.out.println(e);

      }

      }

      }

    原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/database/279/11335779.shtml

  • 相关阅读:
    ASP.Net无法连接Oracle的一个案例
    给Oracle添加split和splitstr函数
    笨猪大改造
    设计模式(一)策略模式
    jQuery select 操作全集
    现在的心情
    jquery 自动实现autocomplete+ajax
    c# 配置连接 mysql
    jquery.ajax和Ajax 获取数据
    C# 加密可逆
  • 原文地址:https://www.cnblogs.com/liulu/p/1832391.html
Copyright © 2011-2022 走看看