简体   繁体   中英

Split contents of file into Array[String] in Scala

I have a SQL file that contains several queries. Since it is a SQL file, it has queries delimited with semicolon (;). I want to read the SQL file and have the queries as an Array[String] in Scala.

For example, I have queries.sql file that contains queries like:

select * from table1;
select col1,col2,col3 from table1 where col1 = ''
col2 = '';
select count(*) from table;

I want the output to look like this:

Array("select * from table1","select col1,col2,col3 from table1 where col1 = '' col2 =' '","select count(*) from table")

You might want to try this:

import scala.io.Source
val theArrayYouWant = Source.fromFile(<filename>).getLines.mkString.split(";")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM