您的位置:首页 > 其它

scala 文件io和md5实例

2015-12-18 18:25 330 查看
package demo

import org.apache.commons.codec.digest.DigestUtils

/**
* Created by Administrator on 2015/12/18.
*/

/**
类名 - 对于所有的类名的第一个字母要大写。
方法名称 - 所有的方法名称的第一个字母用小写。
*/
object TestMain {

class Student{
var username="";
var age=0;
override def toString():String={
return username+age;
}
}
//Unit为没有返回值
def fuck(a:Int): Int =
{
println("a="+a)
return a;
}
//String为返回值
def  sayHello(line:String): String ={

println("sayHello "+line)
return line;
}
def main(args: Array[String]) {
println("xxx");
val xx=fuck(12345)
println(xx)
val md5=DigestUtils.md5Hex("1024");
println(md5)
println(sayHello("dddd"))
val xxx=new Student;
xxx.age=1024;
xxx.username="fuck";
print(xxx.toString())

}

}


package demo

import java.io.{File, PrintWriter}

import scala.collection.immutable.SortedMap
import scala.io.Source
import scala.reflect.io.Path

/**
* Created by Administrator on 2015/12/18.
*/
object FileUtil {

val fnm = "d:/test.txt"

def writeFileDemo(p: Iterator[Path]) {
import java.io._
val writer = new PrintWriter(new File(fnm), "UTF-8")
var i = 0
p.foreach { f =>
i += 1
writer.write(i + "," + f + "\n")
}
writer.close
}

//无参函数
def readFileDemo = {
var sm = SortedMap[String, Int]()
val s = Source.fromFile(fnm, "UTF-8").getLines.foreach { s =>
val ss = s.split(",")
sm += ss(1) -> ss(0).toInt
}
//返回
sm
}

def main(args: Array[String]) {

var lstFile = {
val path = "D:\\data"
Path(path).walkFilter(p => p.isDirectory || ".*.txt".r.findFirstIn(p.name).isDefined)
}
writeFileDemo(lstFile)
readFileDemo.foreach(f => println(f))

//写入文件
val writer = new PrintWriter(new File("test.txt"))
writer.write("Hello Scala \n")
writer.close()

//读取文件
Source.fromFile("test.txt").foreach {
print
}

print("Please enter your input : ")
val line = Console.readLine //c#写法
println("Thanks, you just typed: " + line)

}

}


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>scala-integration</groupId>
<artifactId>scala-integration</artifactId>
<version>1.0</version>

<properties>
<jetty.version>8.1.12.v20130726</jetty.version>
<spring.version>3.2.0.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>central</id>
<url> http://central.maven.org/maven2/ </url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-swing</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.scalaz</groupId>
<artifactId>scalaz-core_2.10</artifactId>
<version>7.1.3</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- httpClient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/libs</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<includes>
<include>**/*.java</include>
<include>**/*.scala</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>manifest</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo file="target/runjetty.bat">
java -cp "libs/*;scala-integration-1.0.jar" weblog.examples.scala.JettyLauncher
</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: