Skip to content

Commit 6a0a6a1

Browse files
committed
17 . 使用Flowable读取一个txt文件(实际业务中文件可能会很大)到内存,一行一行的读取。
1 parent a8e84b1 commit 6a0a6a1

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

learn-sample1/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ android {
66
defaultConfig {
77
applicationId "com.liuh.learn.sample1"
88
minSdkVersion 17
9-
targetSdkVersion 28
9+
targetSdkVersion 22
1010
versionCode 1
1111
versionName "1.0"
1212

learn-sample1/src/main/java/com/liuh/learn/sample1/MainActivity.java

+75-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.liuh.learn.sample1;
22

3+
import android.os.Environment;
34
import android.support.v7.app.AppCompatActivity;
45
import android.os.Bundle;
56
import android.util.Log;
@@ -8,6 +9,12 @@
89
import org.reactivestreams.Subscriber;
910
import org.reactivestreams.Subscription;
1011

12+
import java.io.BufferedReader;
13+
import java.io.FileReader;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.io.InputStreamReader;
17+
1118
import butterknife.ButterKnife;
1219
import butterknife.OnClick;
1320
import io.reactivex.BackpressureStrategy;
@@ -49,7 +56,7 @@ protected void onCreate(Bundle savedInstanceState) {
4956
}
5057

5158
@OnClick({R.id.btn_flowable_test, R.id.btn_flowable_test_synchro, R.id.btn_flowable_test_asynchronous,
52-
R.id.btn_flowable_test_asynchronous2})
59+
R.id.btn_flowable_test_asynchronous2, R.id.btn_flowable_test_readfile_from_sd})
5360
void onViewClicked(View view) {
5461
switch (view.getId()) {
5562
case R.id.btn_flowable_test:
@@ -64,6 +71,9 @@ void onViewClicked(View view) {
6471
case R.id.btn_flowable_test_asynchronous2:
6572
demo_flowable_asynchronous2();
6673
break;
74+
case R.id.btn_flowable_test_readfile_from_sd:
75+
practice1();
76+
break;
6777
}
6878
}
6979

@@ -262,4 +272,68 @@ public void onComplete() {
262272
});
263273
}
264274

275+
276+
public void practice1() {
277+
Flowable.create(new FlowableOnSubscribe<String>() {
278+
@Override
279+
public void subscribe(FlowableEmitter<String> emitter) throws Exception {
280+
281+
try {
282+
283+
InputStream inputStream = getResources().openRawResource(R.raw.kangqiao);
284+
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
285+
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
286+
287+
String str;
288+
289+
while ((str = bufferedReader.readLine()) != null && !emitter.isCancelled()) {
290+
while (emitter.requested() == 0) {
291+
if (emitter.isCancelled()) {
292+
break;
293+
}
294+
}
295+
emitter.onNext(str);
296+
}
297+
bufferedReader.close();
298+
inputStreamReader.close();
299+
inputStream.close();
300+
301+
emitter.onComplete();
302+
} catch (IOException e) {
303+
e.printStackTrace();
304+
}
305+
}
306+
}, BackpressureStrategy.ERROR)
307+
.subscribeOn(Schedulers.io())
308+
.observeOn(AndroidSchedulers.mainThread())
309+
.subscribe(new Subscriber<String>() {
310+
@Override
311+
public void onSubscribe(Subscription s) {
312+
mSubscription = s;
313+
mSubscription.request(1);
314+
}
315+
316+
@Override
317+
public void onNext(String s) {
318+
System.out.println(s);
319+
try {
320+
Thread.sleep(2000);
321+
mSubscription.request(1);
322+
} catch (InterruptedException e) {
323+
e.printStackTrace();
324+
}
325+
}
326+
327+
@Override
328+
public void onError(Throwable t) {
329+
System.out.println(t);
330+
}
331+
332+
@Override
333+
public void onComplete() {
334+
335+
}
336+
});
337+
}
338+
265339
}

learn-sample1/src/main/res/layout/activity_main.xml

+6
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@
3131
android:layout_height="wrap_content"
3232
android:text="request_flowable_异步的2" />
3333

34+
<Button
35+
android:id="@+id/btn_flowable_test_readfile_from_sd"
36+
android:layout_width="match_parent"
37+
android:layout_height="wrap_content"
38+
android:text="从sd卡上读取一个文件,一行一行读取" />
39+
3440
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
�ٱ���
2+
3+
���ߣ���־Ħ
4+
5+
����������ˣ�
6+
���������������
7+
����������֣�
8+
����������Ʋʡ�
9+
10+
�Ǻ��ϵĽ�����
11+
��Ϧ���е����
12+
���������Ӱ��
13+
���ҵ���ͷ������
14+
15+
�����ϵ�������
16+
���͵���ˮ����ҡ��
17+
�ڿ��ӵ��Შ�
18+
�Ҹ�����һ��ˮ�ݣ�
19+
20+
�������µ�һ̶��
21+
������Ȫ�������Ϻ磻
22+
�����ڸ���䣬
23+
�����Ųʺ��Ƶ��Ρ�
24+
25+
Ѱ�Σ���һ֧���ݣ�
26+
����ݸ��ദ���ݣ�
27+
����һ���ǻԣ�
28+
���ǻ԰����Ÿ衣
29+
30+
���Ҳ��ܷŸ裬
31+
�����DZ�������
32+
�ij�ҲΪ�ҳ�Ĭ��
33+
��Ĭ�ǽ����Ŀ��ţ�
34+
35+
���ĵ������ˣ�
36+
���������ĵ�����
37+
�һ�һ�����䣬
38+
������һƬ�Ʋʡ�

0 commit comments

Comments
 (0)