Java 重定向 无法写入_java IO 文件读入,写入,重定向

Java代码 icon_star.png

spinner.gifpackagestar20110526;

importjava.io.BufferedInputStream;

importjava.io.BufferedOutputStream;

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.FileReader;

importjava.io.IOException;

importjava.io.InputStreamReader;

importjava.io.PrintStream;

importjava.io.PrintWriter;

publicclassRedirecting {

publicstaticvoidmain(String[] args)throwsIOException {

redirect();

//      readFile();

//      write("B05\\star20110526\\Redirecting.txt", read());

}

privatestaticvoidredirect()throwsIOException{

//将输出定位到PrintStrem,将 System.out重新定义ouputStream

PrintStream printStream = System.out;

//定义输入流

BufferedInputStream bufferedInputStream = newBufferedInputStream(

newFileInputStream("B05\\star20110526\\Redirecting.java"));

//定义输出流

PrintStream printStream2 = newPrintStream(

newBufferedOutputStream(newFileOutputStream("B05\\star20110526\\Redirect.out")));

//本来System.in是接受控制台参数的,现在将System.in重新定义到bufferedInputStream

System.setIn(bufferedInputStream);

System.setErr(printStream2);

System.setOut(printStream2);

//InputStreamReader 为适配器,转换inputStream到Reader,你懂的

BufferedReader bufferedReader = newBufferedReader(newInputStreamReader(System.in));

String string;

while((string = bufferedReader.readLine()) !=null){

System.out.println(string);

}

printStream.close();

printStream2.close();

bufferedReader.close();

System.setOut(printStream);

}

/**

* 读取文件,然后直接在控制台打印出来

* @throws IOException

*/

privatestaticvoidreadFile()throwsIOException{

File file = newFile("B05\\star20110526\\Redirecting.java");

FileReader fileReader = newFileReader(file);

BufferedReader bufferedReader = newBufferedReader(fileReader);

inti =1;

String string;

while((string = bufferedReader.readLine()) !=null){

System.out.println(i++ +":"+string);

}

bufferedReader.close();

}

/**

* 读取文件,返回文件内容,提供文件写入操作

* @return

* @throws IOException

*/

privatestaticString read()throwsIOException{

File file = newFile("B05\\star20110526\\Redirecting.java");

BufferedReader bufferedReader = newBufferedReader(

newFileReader(file));

StringBuilder stringBuilder = newStringBuilder();

String string;

while((string  = bufferedReader.readLine()) !=null){

stringBuilder.append(string+"\n");

}

returnstringBuilder.toString();

}

/**

* 新建一个文件,然后往文件里面写文件咯

* @param file

* @param text

* @throws FileNotFoundException

*/

privatestaticvoidwrite(String file,String text)throwsFileNotFoundException{

File file2 = newFile(file);

PrintWriter printWriter = newPrintWriter(file2);

printWriter.write(text);

//必须关闭流,要不然出不来结果的,(╰_╯)#(╰_╯)#(╰_╯)#

printWriter.close();

}

}package star20110526;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintStream;

import java.io.PrintWriter;

public class Redirecting {

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

redirect();

//readFile();

//write("B05\\star20110526\\Redirecting.txt", read());

}

private static void redirect() throws IOException{

//将输出定位到PrintStrem,将 System.out重新定义ouputStream

PrintStream printStream = System.out;

//定义输入流

BufferedInputStream bufferedInputStream = new BufferedInputStream(

new FileInputStream("B05\\star20110526\\Redirecting.java"));

//定义输出流

PrintStream printStream2 = new PrintStream(

new BufferedOutputStream(new FileOutputStream("B05\\star20110526\\Redirect.out")));

//本来System.in是接受控制台参数的,现在将System.in重新定义到bufferedInputStream

System.setIn(bufferedInputStream);

System.setErr(printStream2);

System.setOut(printStream2);

//InputStreamReader 为适配器,转换inputStream到Reader,你懂的

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

String string;

while((string = bufferedReader.readLine()) != null){

System.out.println(string);

}

printStream.close();

printStream2.close();

bufferedReader.close();

System.setOut(printStream);

}

/**

* 读取文件,然后直接在控制台打印出来

* @throws IOException

*/

private static void readFile() throws IOException{

File file = new File("B05\\star20110526\\Redirecting.java");

FileReader fileReader = new FileReader(file);

BufferedReader bufferedReader = new BufferedReader(fileReader);

int i = 1;

String string;

while((string = bufferedReader.readLine()) != null){

System.out.println(i++ +":"+string);

}

bufferedReader.close();

}

/**

* 读取文件,返回文件内容,提供文件写入操作

* @return

* @throws IOException

*/

private static String read() throws IOException{

File file = new File("B05\\star20110526\\Redirecting.java");

BufferedReader bufferedReader = new BufferedReader(

new FileReader(file));

StringBuilder stringBuilder = new StringBuilder();

String string;

while((string = bufferedReader.readLine()) != null){

stringBuilder.append(string+"\n");

}

return stringBuilder.toString();

}

/**

* 新建一个文件,然后往文件里面写文件咯

* @param file

* @param text

* @throws FileNotFoundException

*/

private static void write(String file,String text) throws FileNotFoundException{

File file2 = new File(file);

PrintWriter printWriter = new PrintWriter(file2);

printWriter.write(text);

//必须关闭流,要不然出不来结果的,(╰_╯)#(╰_╯)#(╰_╯)#

printWriter.close();

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441081.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

题干&#xff1a; FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 < p < n and 0 < q < n. At each grid location Fatmouse has hid between 0 and 10…

获取excel名称java_使用Apache POI获取大型Excel文件的Excel工作表名称

小编典典为了显示Gagravarr的评论可能意味着什么&#xff1a;该XSSFReader包含方法XSSFReader.getSheetsData其中“返回一个迭代器&#xff0c;这将让你在把所有的不同的表&#xff0c;每个表的InputStream中只有打开时开始迭代器牵强。这是给你的时候&#xff0c;每个做关闭In…

【CodeForces - 298C】Parity Game (思维,有坑)

题干&#xff1a; You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and &q…

java求小数高精度_浅谈Java中的高精度整数和高精度小数

在实际编码中&#xff0c;会遇到很多高精度的事例&#xff0c;比如&#xff0c;在计算金钱的时候就需要保留高精度小数&#xff0c;这样计算才不会有太大误差&#xff1a;在下面的代码中&#xff0c;我们验证了&#xff0c;当两个float型的数字相加&#xff0c;得到的结果和我们…

【CodeForces - 298B 】Sail (模拟,题意)

题干&#xff1a; The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently…

java中能构成循环的语句_《编程导论(Java)#183;3.2.4 循环语句》

本文全然复制《编程导论(Java)3.2.4 循环语句》的内容。除【】中的说明文字。请阅读和比較其它编程教材。我知道。假设我是一个刚開始学习的人&#xff0c;《编程导论(Java)》非常不适合自学。建议同学们阅读时&#xff0c;一定选择一本其它的书同一时候看&#xff0c;或上网。…

【CodeForces - 340B 】Maximal Area Quadrilateral (计算几何,枚举,有坑)

题干&#xff1a; Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners)…

php网站的编辑器,5款适合PHP使用的HTML编辑器推荐

概述Web开发中&#xff0c;很多地方都会用到HTML编辑器(富文本编辑器)&#xff0c;我也用过几种&#xff0c;如UEditor、CkEditor等。这几天看了几篇文章&#xff0c;都是关于HTML编辑器的&#xff0c;写个文章记录下。推荐的编辑器simditor这个编辑器是前几天刚看到的&#xf…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级 )】小乐乐和25(模拟,技巧)

题干&#xff1a; 小乐乐特别喜欢25这个数字&#xff0c;他想把所有的数字都变成25的倍数。 现在小乐乐得到一个数字&#xff0c;想问问你最少用几次操作才可以把这个数字改造成25的倍数。 对于一次操作我们可以把相邻的两位做交换&#xff0c;比如123经过一次操作之后就可以…

怎么表示一个PHP语句块,php switch语句多个值匹配同一代码块应用示例

先说说switch()语句的格式switch(表达式){case 匹配1&#xff1a;当匹配1和表达式匹配成功执行的代码;break;case 匹配2&#xff1a;当匹配2和表达式匹配成功执行的代码;break;default&#xff1a;如果case语句没有与表达式成功所执行的代码;}理解 switch 是怎样执行的非常重要…

php查找顶级分类,php 无限级分类 获取顶级分类ID,php顶级_PHP教程

php 无限级分类 获取顶级分类ID&#xff0c;php顶级有这样一个表&#xff0c;id是分类的ID&#xff0c;name是分类名称&#xff0c;pid是上级分类的ID。现在有个分类ID&#xff0c;程序要找到它上级的上级的上级……分类的ID&#xff0c;简单说就是找出顶级分类的ID。比如“新鲜…

【POJ - 2392】Space Elevator (dp,优秀的背包问题)

题干&#xff1a; The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 < K < 400) different types of blocks with which to build the tower. Each block of type i has heigh…

php三种web开发技术,三种WEB开发主流技术ASP-PHP-JSP的评价

三种Web开发主流技术ASP-PHP-JSP的评价[摘要]本文从程序开发者的角度&#xff0c;对现今社会盛行的WEB开发技术进行了分析&#xff0c;旨在让WEB 开发人员、使用者、准备学习的人对WEB开发技术有更加深入的了解&#xff0c;并为WEB应用程序开发提供指导。[关键词]WEB开发技术 A…

*【CodeForces - 1088 ABC】套题比赛,A水题B模拟C构造D交互

A. Input The only line contains the integer xx (1≤x≤100)(1≤x≤100). Output You should output two integers aa and bb, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (wit…

php 计算前几天,php计算几分钟前、几小时前、几天前的几个函数、类分享

搜索热词一、函数实现实例1&#xff1a;代码如下:function time_tran($the_time){$now_time date("Y-m-d H:i:s",time()8*60*60);$now_time strtotime($now_time);$show_time strtotime($the_time);$dur $now_time - $show_time;if($dur < 0){return $the_tim…

【CodeForces - 608D】Zuma(区间dp)

题干&#xff1a; Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible. In one second, Geno…

matlab没有int函数,matlab 未定义与 'char' 类型的输入参数相对应的函数 'int'。

最后你那zd句plot(int(k),double(s));里的int(k);不对。你如果想以k为横坐标&#xff0c;直接把int去掉&#xff0c;如回果非想要变成整数可以用floor(k);注&#xff1a;fix&#xff1a;向零取整 floor&#xff1a;向小取整 round&#xff1a;四舍五入 ceil&#xff1a;向...如…

【CodeForces - 347C 】Alice and Bob (思维,数学,等差数列)

题干&#xff1a; It is so boring in the summer holiday, isnt it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each …

java读取yaml配置文件,snakeyaml读取yaml配置文件

下面上我解析yaml文件的例子&#xff1a;// me.yamlage: 1name: jiaobuchongparams:event: whats upurl: http://www.jiaobuchong.comfavoriteBooks:- Gone with the wind- The Little Prince对应的JavaBean类&#xff1a;// Me.javapublic class Me {private Integer age;priv…

算法讲解 -- 区间dp经典模型与优化(石子归并)

石子合并问题是最经典的DP问题。首先它有如下3种题型&#xff1a; PPT讲解&#xff1a;点击打开链接 (1)有N堆石子&#xff0c;现要将石子有序的合并成一堆&#xff0c;规定如下&#xff1a;每次只能移动任意的2堆石子合并&#xff0c;合并花费为新合成的一堆石子的数量。求将…