Python 脚本参数

# Primer argumento al script
sys.argv[1]

Python Copiar ficheros sobreescribiendo

import shutil

shutil.copyfile(origen,destino)

Python Sobreescribir todos los ficheros de cierto nombre en una ruta

import os
import os.path
import sys
import shutil

def copytodir(origin,dir):
	files = os.listdir(dir)

	for file in files:
		#file.lower()
		
		#print(file)
		
		# Si es un video.html sobreescribirlo con el fichero origen
		if (file == "Video.htm"):
			print("origin: " + origin + " to " + dir + "/" + "Video.htm")
			
			shutil.copyfile(origin, dir + "/" + "Video.htm")

		# Si es un directorio entrar dentro a buscar un video.html
		if (os.path.isdir(dir + "/" + file)):
			copytodir(origin,dir + "/" + file)
			


def main(dir):
	files = os.listdir(".")
	#print (files)
	encontrado = False
	
	for file in files:
		if (file == "Video.htm"):
			encontrado = True
	if (encontrado):
		copytodir("./Video.htm",dir)
	else:
		print("No he encontrado el archivo origen Video.htm")			
	


if __name__ == "__main__":
    main(sys.argv[1])

Python Servidor de echo

import socket
import sys

host = ''
port = 50000
backlog = 5
size = 1024
s = None
try:
	s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	s.bind((host,port))
	s.listen(backlog)
except socket.error,(value,message):
	if s:
		s.close()
	print "Could not open socket: " + message
	sys.exit(1)
while 1:
	client,address = s.accept()
	data = client.recv(size)
	if data:
		print(data)
		client.send(data)
		if (data=="quit"):
			sys.exit(0)
	client.close()

Python Cliente de echo

import socket
import sys

host = 'localhost'
port = 50000
size = 1024
s = None

if (len(sys.argv)>0):
	message = sys.argv[1]
else:
	message = "Hello, World!"

try:
	s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	s.connect((host,port))
except socket.error,(value,errmessage):
	if s:
		s.close()
	print "Could not open socket: " + errmessage
	sys.exit(1)
s.send(message)	
data = s.recv(size)
s.close()
print 'Received: ',data

Python Númerodeargumentos pasados

len(sys.argv)

Java 泛型 - 使用类型通配符

public void printList(List<?> list, PrintStream out) throws IOException {

      for (Iterator<?> i = list.iterator( ); i.hasNext( ); ) {

        out.println(i.next( ).toString( ));

      }

 }

Java 泛型 - 未知清单

public interface List<E> {



      public E get( );

      

      public void add(E value);

    }

Java 泛型 - 未知列表 - 添加类型限制。

public class NumberBox<N extends Number> extends Box<N> {



  public NumberBox( ) {

    super( );

  }

  

  // Sum everything in the box

  public double sum( ) {

    double total = 0;

    for (Iterator<N> i = contents.iterator( ); i.hasNext( ); ) {

      total = total + i.next( ).doubleValue( );

    }

    return total;

  }

}

You can use this same syntax in method definitions:

    public static double sum(Box<? extends Number> box1,

                             Box<? extends Number> box2) {

      double total = 0;

      for (Iterator<? extends Number> i = box1.contents.iterator( );

           i.hasNext( ); ) {

        total = total + i.next( ).doubleValue( );

      }

      for (Iterator<? extends Number> i = box2.contents.iterator( );

           i.hasNext( ); ) {

        total = total + i.next( ).doubleValue( );

      }

      return total;

    }

Java 打开枚举

public void testSwitchStatement(PrintStream out) throws IOException {

     StringBuffer outputText = new StringBuffer(student1.getFullName( ));

      

     switch (student1.getGrade( )) {

       case A:

         outputText.append(" excelled with a grade of A");

         break;

       case B: // fall through to C

       case C:

         outputText.append(" passed with a grade of ")

                    .append(student1.getGrade( ).toString( ));

         break;

       case D: // fall through to F

       case F:

         outputText.append(" failed with a grade of ")

                    .append(student1.getGrade( ).toString( ));

         break;

       case INCOMPLETE:

         outputText.append(" did not complete the class.");

         break;

     }

         

     out.println(outputText.toString( ));

   }