public class ReadByteStream { public static void main(String[] args) { //stream 字节流 //reader,writer 字符流 //input 读取外部东西, //output 输出 try { //read byte stream,mytext.txt is exits FileInputStream inputStream=new FileInputStream("mytext.txt"); byte []in=new byte[60]; inputStream.read(in);//把字节读入数组中 //String str=new String(in);默认是GBK String str=new String(in,"UTF-8");//是乱码 System.out.println(str); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class WriteByteStream { public static void main(String[] args) { try { FileOutputStream fileOutputStream=new FileOutputStream("mytext1.txt"); String str="hello ,jack..."; byte []out=str.getBytes(); try { fileOutputStream.write(out); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
public class CopyByByteStream { public static void main(String[] args) { try { FileInputStream inputStream=new FileInputStream("water.jpg"); FileOutputStream outputStream=new FileOutputStream("newwater.jpg"); byte []in=new byte[50]; //现在只是读取了50个字节,不能读取完整的图片 // inputStream.read(in); //不能读取完整的图片 // outputStream.write(in); // inputStream.read(in);返回int类型,如果返回-1表示没有数据了 while(inputStream.read(in)!=-1)//表示还有数据,就写入 { outputStream.write(in); } inputStream.close(); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
public class FileReaderAndFileWriter { public static void main(String[] args) { try { //文件写入读取 FileReader fileReader=new FileReader("java.txt"); BufferedReader bufferedReader=new BufferedReader(fileReader); //字符流包装为缓冲流 FileWriter fileWriter=new FileWriter("java_new_1"); BufferedWriter bufferedWriter=new BufferedWriter(fileWriter); String len; while ((len=bufferedReader.readLine())!=null) { bufferedWriter.write(len+"\n"); } bufferedWriter.flush();//注意writer有关的都要flush() bufferedWriter.close(); bufferedReader.close(); fileReader.close(); fileWriter.close(); System.out.println("Done"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
public class BufferReader { public static void main(String[] args) { try { FileInputStream fileInputStream=new FileInputStream("java.txt"); InputStreamReader isr=new InputStreamReader(fileInputStream);//字节流转化为字符流 BufferedReader reader=new BufferedReader(isr); FileOutputStream fileOutputStream=new FileOutputStream("java_new_buffered.txt"); OutputStreamWriter osw=new OutputStreamWriter(fileOutputStream); BufferedWriter writer=new BufferedWriter(osw); String len; while ((len=reader.readLine())!=null) { writer.write(len); } writer.flush(); writer.close(); osw.close(); fileInputStream.close(); fileOutputStream.close(); System.out.println("done"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class ReadBufferByStream { public static void main(String[] args) { try { FileInputStream fileInputStream=new FileInputStream("vidio.wmv"); BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream); FileOutputStream fileOutputStream=new FileOutputStream("E://TextVidio.wmv"); BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream); byte []by=new byte[100]; while ( bufferedInputStream.read(by)!=-1) { bufferedOutputStream.write(by); } bufferedInputStream.close(); bufferedOutputStream.close(); fileInputStream.close(); fileOutputStream.close(); System.out.println("vidio create sucess...."); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
时间: 2024-10-13 23:00:16