Last active
August 29, 2015 14:24
-
-
Save AARomanov1985/5b10e661c72ca2c695a0 to your computer and use it in GitHub Desktop.
Java I/O
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package java_core.io.raw; | |
import java.io.*; | |
/** | |
* | |
* @author AARomanov1985 | |
*/ | |
public class DataIO { | |
public static void main(String args[]) throws IOException { | |
DataOutputStream out = new DataOutputStream(new FileOutputStream("data.dat")) | |
out.writeUTF("Это тестовая строка"); | |
out.writeInt(100); | |
out.writeDouble(12345.7676); | |
out.writeBoolean(true); | |
DataInputStream in = new DataInputStream(new FileInputStream("data.dat")) | |
System.out.println(in.readUTF()); | |
System.out.println(in.readInt()); | |
System.out.println(in.readDouble()); | |
System.out.println(in.readBoolean()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package java_core.io.raw; | |
import java.io.*; | |
import java.util.*; | |
/** | |
* | |
* @author AARomanov1985 | |
*/ | |
public class DirList { | |
public static void main(String args[]){ | |
for (String s : dirList("d:\\")){ | |
System.out.println(s); | |
} | |
} | |
public static String[] dirList(String path){ | |
File dir = new File(path); | |
String[] list = dir.list(); | |
Arrays.sort(list); | |
return list; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package java_core.io.raw; | |
import java.io.*; | |
import java.nio.channels.*; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* Установка блокировки на файл | |
*/ | |
public class FileBlock { | |
public static void main(String[] args) throws Exception{ | |
try (FileOutputStream fos = new FileOutputStream("file.dat")) { | |
FileLock fl = fos.getChannel().tryLock(); | |
if (fl !=null){ | |
System.out.println("Файл заблокирован"); | |
TimeUnit.MICROSECONDS.sleep(100); | |
fl.release(); | |
System.out.println("Блокировка снята"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package java_core.io.raw; | |
import java.io.*; | |
import java.nio.*; | |
import java.nio.channels.*; | |
import java.util.logging.*; | |
/** | |
* | |
* @author AARomanov1985 | |
*/ | |
public class FileCopyNIO { | |
private static final int BSIZE = 65536*256; | |
private static boolean copy(String filename, String copyname) { | |
ByteBuffer buffer = ByteBuffer.allocate(BSIZE); | |
try (FileChannel in = new FileInputStream(filename).getChannel(); | |
FileChannel out = new FileOutputStream(copyname).getChannel();) { | |
long available; | |
while ((available = in.size() - in.position()) > 0) { | |
if (available < BSIZE) { | |
buffer = ByteBuffer.allocate((int) available); | |
} | |
in.read(buffer); | |
buffer.flip(); | |
out.write(buffer); | |
buffer.clear(); | |
} | |
} catch (IOException e) { | |
Logger.getGlobal().severe(e.getMessage()); | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package java_core.io.raw; | |
import java.io.*; | |
/** | |
* | |
* @author AARomanov1985 | |
*/ | |
public class MarkTest { | |
public static void main(String args[])throws IOException{ | |
BufferedReader in = new BufferedReader(new FileReader("in.txt")); | |
read(in); | |
in.reset(); | |
System.out.println("Сброс"); | |
read(in); | |
} | |
private static void read(BufferedReader in) throws IOException{ | |
int c; | |
while ((c=in.read())!=-1){ | |
System.out.println((char)c); | |
if ((char)c=='\n'){ | |
in.mark(c); | |
System.out.println("Метка!"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package java_core.io.serial.raw.support; | |
import java.io.*; | |
/** | |
* Пример расширенной сериализации | |
*/ | |
public class SimpleTimer implements Externalizable{ | |
private byte hour; | |
private byte min; | |
private byte sec; | |
public SimpleTimer(){ | |
} | |
public SimpleTimer(String time){ | |
this.hour = (byte)Integer.parseInt(time.substring(0, 2)); | |
this.min = (byte)Integer.parseInt(time.substring(3, 5)); | |
this.sec = (byte)Integer.parseInt(time.substring(6, 8)); | |
} | |
@Override | |
public void writeExternal(ObjectOutput out) throws IOException { | |
out.write(hour); | |
out.write(min); | |
out.write(sec); | |
} | |
@Override | |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { | |
this.hour = in.readByte(); | |
this.min = in.readByte(); | |
this.sec = in.readByte(); | |
} | |
public int getHour() { | |
return hour; | |
} | |
public void setHour(byte hour) { | |
this.hour = hour; | |
} | |
public int getMin() { | |
return min; | |
} | |
public void setMin(byte min) { | |
this.min = min; | |
} | |
public int getSec() { | |
return sec; | |
} | |
public void setSec(byte sec) { | |
this.sec = sec; | |
} | |
public static void main(String[] args)throws IOException, ClassNotFoundException{ | |
SimpleTimer timer = new SimpleTimer("14:59:13"); | |
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("SimpleTimer.data")); | |
out.writeObject(timer); | |
ObjectInputStream in = new ObjectInputStream(new FileInputStream("SimpleTimer.data")); | |
SimpleTimer timer2 = (SimpleTimer)in.readObject(); | |
System.out.println(timer2.getHour()+":"+timer2.getMin()+":"+timer2.getSec()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package java_core.io.serial.raw.support; | |
import java.io.*; | |
/** | |
* Сериализация полей, помеченных transient | |
*/ | |
public class SimpleTimerTransient implements Serializable{ | |
private transient String Name; | |
private byte hour; | |
private byte min; | |
private byte sec; | |
public SimpleTimerTransient(String time){ | |
this.hour = (byte)Integer.parseInt(time.substring(0, 2)); | |
this.min = (byte)Integer.parseInt(time.substring(3, 5)); | |
this.sec = (byte)Integer.parseInt(time.substring(6, 8)); | |
} | |
private void writeObject(ObjectOutputStream out) throws IOException { | |
out.defaultWriteObject(); // Запуск сериализации по-умолчанию | |
out.writeUTF(Name); // Запись transient-поля вручную | |
} | |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { | |
in.defaultReadObject(); // Запуск десериализации по-умолчанию | |
this.Name = in.readUTF(); // Восстановление transient-поля вручную | |
} | |
/* | |
Игнорирование обычной сериализации: | |
private void writeObject(ObjectOutputStream out) throws IOException { | |
out.writeUTF(Name); | |
out.write(hour); | |
out.write(min); | |
out.write(sec); | |
} | |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { | |
this.Name = in.readUTF(); | |
this.hour = in.readByte(); | |
this.min = in.readByte(); | |
this.sec = in.readByte(); | |
} | |
*/ | |
public int getHour() { | |
return hour; | |
} | |
public void setHour(byte hour) { | |
this.hour = hour; | |
} | |
public int getMin() { | |
return min; | |
} | |
public void setMin(byte min) { | |
this.min = min; | |
} | |
public int getSec() { | |
return sec; | |
} | |
public void setSec(byte sec) { | |
this.sec = sec; | |
} | |
public String getName() { | |
return Name; | |
} | |
public void setName(String Name) { | |
this.Name = Name; | |
} | |
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { | |
SimpleTimerTransient timer = new SimpleTimerTransient("15:22:16"); | |
timer.setName("Таймер"); | |
System.out.println(timer.getHour()+":"+timer.getMin()+":"+timer.getSec()); | |
System.out.println(timer.getName()); | |
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("SimpleTimer.data")); | |
out.writeObject(timer); | |
ObjectInputStream in = new ObjectInputStream(new FileInputStream("SimpleTimer.data")); | |
SimpleTimerTransient timer2 = (SimpleTimerTransient)in.readObject(); | |
System.out.println(timer2.getHour()+":"+timer2.getMin()+":"+timer2.getSec()); | |
System.out.println(timer2.getName()); | |
} | |
} | |
/* Вывод: | |
15:22:16 | |
Таймер | |
15:22:16 | |
Таймер | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package java_core.io.raw; | |
import java.io.*; | |
import java.net.*; | |
import java.util.*; | |
/** | |
* | |
* Пример простого сервера на порте 8189 | |
*/ | |
public class ThreadedEchoServer { | |
public static void main(String[] args) { | |
try { | |
int i = 1; | |
ServerSocket s = new ServerSocket(8189); | |
while (true) { | |
Socket in = s.accept(); | |
System.out.println("Spawing "+i); | |
Runnable r = new ThreadedEchoHandler(in); | |
Thread t = new Thread(r); | |
t.start(); | |
i++; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class ThreadedEchoHandler implements Runnable{ | |
private Socket in; | |
ThreadedEchoHandler(Socket in){ | |
this.in = in; | |
} | |
@Override | |
public void run() { | |
try{ | |
try{ | |
InputStream ins = in.getInputStream(); | |
OutputStream ous = in.getOutputStream(); | |
Scanner sc = new Scanner(ins); | |
PrintWriter out = new PrintWriter(ous, true); | |
out.println("Hello! Enter BYE to exit."); | |
while(sc.hasNextLine()){ | |
String line = sc.nextLine(); | |
out.println("Echo: "+line); | |
if (line.equals("BYE")){ | |
break; | |
} | |
} | |
}finally{ | |
in.close(); | |
} | |
}catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment