Java Programming Idea--I/O system __ algorithm

Source: Internet
Author: User
Tags readline stdin stub

1,file class

Represents the name of a particular file and the name of a group of files under a directory. If it points to a set of files, you can call the list () method with the collection of words and return an array of characters.

Example 1

Package filetest;

Import Java.io.File;

public class Dirlist {

	/**
	 * @param args */public
	static void Main (string[] args) {
		//TODO Auto-gene Rated method stub
		file Filedir=new file ("/home/hadoop/data/lucenedata");
		File[] Textfiles=filedir.listfiles ();
		for (File file:textfiles) {
			System.out.println (File.getabsolutepath ());
			System.out.println (File.getname ());}}

Results

/home/hadoop/data/lucenedata/txt1.txt
txt1.txt
/home/hadoop/data/lucenedata/txt2.txt
txt2.txt
/home/hadoop/data/lucenedata/txt5.txt
txt5.txt
/home/hadoop/data/lucenedata/txt4.txt
Txt4.txt
/home/hadoop/data/lucenedata/txt3.txt
txt3.txt
Absolute path and filename of output file

2, input and output

Buffer input File

Package inoutput;

Import Java.io.BufferedReader;
Import Java.io.FileReader;
Import Java.io.IOError;
Import java.io.IOException;

public class Bufferedinputfile {

	/**
	 * @param args
	 *
	/public static String read (String filename) throws I oexception{
		BufferedReader in=new bufferedreader (new FileReader (filename));
		String s;
		StringBuilder SB =new StringBuilder ();
		while ((S=in.readline ())!=null) {
			sb.append (s+ "\ n");
		}
		In.close ();
		return sb.tostring ();
	}
	public static void Main (string[] args) throws IOException {
		//TODO auto-generated method stub
		System.out.print ln (Read ("./src/filetest/dirlist.java"));
	}



Results

Package filetest;

Import Java.io.File;
Import java.io.IOException;

public class Dirlist {

	/**
	 * @param args
	 * @throws ioexception
	/public static void Main (string[) AR GS) throws IOException {
		//TODO auto-generated method stub
		File Filedir=new file ("/home/hadoop/data/lucenedata ");
		File[] Textfiles=filedir.listfiles ();
		for (File file:textfiles) {
			System.out.println (File.getabsolutepath ());
			System.out.println (File.getcanonicalpath ());
			System.out.println (File.getname ());}}



Input from memory

Package inoutput;

Import java.io.IOException;
Import Java.io.StringReader;

public class Memoryinput {

	/**
	 * @param args
	 * @throws ioexception
	/public static void main (String [] args) throws IOException {
		//TODO auto-generated method stub
		StringReader inreader=new StringReader ( Bufferedinputfile.read ("./src/filetest/dirlist.java"));
		int C;
		while ((C=inreader.read ())!=-1) {
			System.out.println ((char) c);
		}
		System.out.println (Bufferedinputfile.read ("./src/filetest/dirlist.java"));
	}



Results

Package inoutput;

Import java.io.IOException;
Import Java.io.StringReader;

public class Memoryinput {

	/**
	 * @param args
	 * @throws ioexception
	/public static void main (String [] args) throws IOException {
		//TODO auto-generated method stub
		StringReader inreader=new StringReader ( Bufferedinputfile.read ("./src/filetest/dirlist.java"));
		int C;
		while ((C=inreader.read ())!=-1) {
			System.out.println ((char) c);
		}
		System.out.println (Bufferedinputfile.read ("./src/filetest/dirlist.java"));
	}



Basic file output

Package inoutput;

Import Java.io.BufferedReader;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.io.StringReader;

public class Basicfileoutput {
	static String file = ' output.out ';
	/**
	 * @param args */public
	static void Main (string[] args) throws ioexception{
		//TODO auto-generated Method stub
		BufferedReader in= new BufferedReader (Bufferedinputfile.read ("./src/filetest/ Dirlist.java "));
		PrintWriter out=new printwriter (file);
		int linecount=1;
		String s;
		while ((S=in.readline ())!=null) {
			out.println (linecount++ + ":" +s);
		}
		Out.close ();
		System.out.println (bufferedinputfile.read (file));//Read System.out.println from memory
		(Bufferedinputfile.read ("./ Output.out "));
	}



Results

1:package filetest;
2:3: Import java.io.File;
4:import java.io.IOException;  5:6: public class Dirlist {7:8:/** 9: * @param args: * @throws ioexception One: * * 12:public static void Main (string[] args) throws IOException {://TODO auto-generated method stub 14:file filedir=new File ("/home/hado
Op/data/lucenedata ");
15:file[] Textfiles=filedir.listfiles (); 16:for (File file:textfiles) {17:system.out.println (File.getabsolutepath ()); 18:system.out.println (File.getCano
Nicalpath ());
19:system.out.println (File.getname ());
21:22:} 23:24:} 1:package filetest;
2:3: Import java.io.File;
4:import java.io.IOException;  5:6: public class Dirlist {7:8:/** 9: * @param args: * @throws ioexception One: * * 12:public static void Main (string[] args) throws IOException {://TODO auto-generated method stub 14:file filedir=new File ("/home/hado
Op/data/lucenedata ");
15:file[] Textfiles=filedir.listfiles (); 		16:for (File file:textfiles) {17:system.out.println (File.getabsolutepath ()); 18:system.out.println (File.getCanonicalP
Ath ());
19:system.out.println (File.getname ()); 20:} 21:22:} 23:24:}

3,i/o

Read from standard input

Package IO;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;

public class Echo {

	/**
	 * @param args
	 * @throws ioexception
	/public static void Main (string[) args) Throws IOException {
		//TODO auto-generated method stub
		bufferedreader stdin =new bufferedreader (New Inputstrea Mreader (system.in));
		String s;
		while ((S=stdin.readline ())!= null && s.length ()!=0) {
			System.out.println (s);
		}
	}

}

Results

haha
haha

Output

Package IO;

Import Java.io.PrintWriter;

public class Changesystemout {

	/**
	 * @param args */public
	static void Main (string[] args) {
		//TODO A uto-generated method Stub
		printwriter out =new printwriter (system.out,true);
		Out.println ("hello,world!");
	}



Results

hello,world!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.