[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

sample[jx8-android] File

Expression to run this page
SwikiCodeRobot @> #JX8AndroidFile

" External storage state. "
self print:  'ExternalStorageState=', Environment externalStorageState.


" Check existence of some files and folders. "
#(	A8 'A8/u8.configuration.json'
	JX8 U8 Android
	'Android/Data/com.skype.raider/cache/com.skype.raider.htrace0'
) do: [:fileName | | file |
	file := File pathName: Environment externalStoragePathName
		,File separator ,fileName.
	self print: 'File ' ,fileName printString
		,' exists=' ,file exists.
	file exists ifTrue: [
		file isDirectory ifTrue: [
			self print: #isDirectory
				",' total=' ,file totalSpace
				,' usable=' ,file usableSpace"
				,' list=' ,file list printString
		].
		file isFile ifTrue: [
			self print: #isFile
				,' length=' ,file length
		].
	]
].


" Search for a readable txt file. "
| search searchFile |
searchFile := [:file|
	((file name fromLast: $.) asLowercase = #txt
	 and: [file canRead]) ifTrue: [
		^self	print: 'Found: ',file absolutePath;
			print: 'Contents: ',file absolutePath fileContents
	].
].
search := [:where | | file |
	file := File pathName: where.
	file isFile ifTrue: [ searchFile value: file ].
	file isDirectory ifTrue: [
		file list sorted do: [:fName|
			search value: where ,File separator ,fName
		]
	].

].
search value: Environment externalStoragePathName.


"Read file contents"
| file reader read chars content |
file := File pathName:
	Environment externalStoragePathName
	,File separator ,'A8/u8.configuration.json'.
file exists ifFalse: [ ^self error: 'File not found' ].
reader := FileReader on: file.
chars := Java current newArrayOf: #char size: file length.
read := reader read: chars.
self print: 'File read ',(read = file length
	ifTrue: [ 'all contents' ]
	ifFalse: [ 'only ' ,read ,'bytes.' ]).
content := JavaString fromChars: chars.
reader close.
"self print: content"


"s8 basic file contents"
| contents |
contents := '*/A8/u8.configuration.json' fileContents.
contents isNil ifTrue: [ ^self error: 'File not found' ].
self print: 'fileContents read ' ,contents size ,' characters.'